Model switching code not working for multiple instances

I have a code that switches one model off and another on in response to a variable, effectively swapping them when the variable changes. I have this code attached to a game-object, with two models as children. However, when duplicating the game object and running the scene, only one instance will work correctly, (usually the most recently duplicated) and the rest will simply display both models at the same time, and not respond to the variable change. Could some one help me understand how to create multiple working instances?

Here’s the code:

var lightsOn: boolean;
    
    var modelOff: GameObject;
    modelOff = GameObject.Find("LightPanel1off");
    var modelOn: GameObject;
    modelOn = GameObject.Find("LightPanel1on");
    function Update(){
    if (lightsOn == false){
    modelOff.SetActiveRecursively(true);
    modelOn.SetActiveRecursively(false);
    }
    if (lightsOn == true){
    modelOff.SetActiveRecursively(false);
    modelOn.SetActiveRecursively(true);
    }
     
    }

Thanksabunch! :slight_smile:

OK, first things first, to make this work, you need to assign your models to your object via the inspector, and not using GameObject.Find - You also have some redundancy in you code.

So your code should look something like this:

var lightsOn: boolean;

// Inspector variables 
var modelOff: GameObject;
var modelOn: GameObject;

function Update(){
      modelOff.SetActiveRecursively(!lightsOn);
      modelOn.SetActiveRecursively(lightsOn);    
}

A couple of programming notes here:

  1. About the if-else statement:
    if takes a boolean statement, i.e.
    a statement that returns either true
    or false - The body of the if will
    get executed if that statement
    returns true, otherwise the body of
    the else is the one to get
    executed, you probably know that.
    Now, lightsOn is a boolean
    variable, it returns either true or
    false right? - Let’s assume that the
    light was turned on (so lightsOn is
    true) - saying if (lightsOn == true) is just like saying if (true == true) - Redundant, right? :slight_smile:
  2. In your code, you wrote:
    if (light was on)
    // do something
    if (light was off)
    // do something else Now tell me, can a light be both on and off
    at the same time? :wink: - This is what
    the else statement is for - using
    an if-else means that it’s either
    this OR that. The way you did it,
    both if statements will get checked,
    if the light was on, the 2nd if is
    redundant because of course it’s not
    off :slight_smile:

A Unity note:
Notice there’s something not right about my code? - I’m calling SetActiveRecursively on both models EVERY frame, why? there’s no need to. All I need to do, is toggle the models upon turning on/off the light, right? - So how can I write something like “Whenever the light changes states (on/off) → Toggle the models”? - o.O

First, here’s your ToggleModels function:

function ToggleModels()
{
      model_one.SetActiveRecursively(!model_one.active);
      model_two.SetActiveRecursively(!model_two.active); 
}

If that confused you, toggling something means switching whatever state it was at before, to the opposite state - so if the the model was ‘on’ - toggling it is switching it ‘off’ - (!on) - if it was ‘off’ toggling it == !off = on :slight_smile:

To answer our previous question, you need to use ‘events’ - Basically you should have an onLightChanges or onLightToggles event that will fire when the light gets turned on/off - Let you object ‘subscribe’ to that event, so that whenever it ‘fires’, it toggles the models :slight_smile:

More about events could be found here, here and here.
Btw I highly advice you to invest the time learning C#.

Oh and, almost forgot, why did your original code not work? - Because GameObject.Find("something"); will search in your entire scene and return the first instance of “something” it finds, if any. - In your case, when you duplicate your ‘object’, the models (children) gets duplicated as well, but with the SAME names, like:

15362-untitled.png

So, GameObject.Find actually did its job pretty darn good, it gave you back the first instances of the models your were looking for :slight_smile: - Which is why, it’s best to make the models public variables in your object script (make your object even a prefab) and assign them through your inspector :slight_smile: