Attaching scripts at runtime

Options

Hello! I want to attach a wireframe generator script to all of the objects in an arbitrary scene. We are trying to rapidly iterate on level designs that need some wireframe materials, and it would be great if we can throw a new level into LS without having to set up the wireframe material every time.

I could do this by attaching the scripts at runtime, but I'm not sure how to set the actual script source, after creating the component.

  // create the script component, obviously...
  var wireframeScript = child.createComponent('Script')
  // now what?

I've wanted to do this in the past, but I ended up using prefabs to accomplish it. That won't work in this case. Here's the old thread, for reference.

https://support.lensstudio.snapchat.com/hc/en-us/community/posts/360061375952-How-to-add-a-specific-script-to-an-instantiated-object-

I love it when I need to know an answer to something and find myself at the end of the only relevant thread that google can find, lol. Any insights will be appreciated. I'm out of ideas!

Answers

  • سعد الفريدي
    سعد الفريدي Posts: 447 🔥🔥🔥
    Options

    Hey @Joshua Beckwith

    Is your version the latest? Lens Studio!

    your script must be in Lens Studio, and the Script must be attached to the camera inside the lens Studio

  • brandon
    brandon Posts: 15 🔥
    edited August 2022 #3
    Options

    I don't think there is a way yet. The closest would probably be swapping the script on a single object then using sceneObject.copyComponent for all the others to copy it with the script asset attached already