I ran into an interesting problem. I was making a simple weapons controller and chose to use SetActive to make sure I only had one weapon active. This should have let all the scripts work on that weapon. Problem was, none of them worked.
#pragma strict
import System.Collections.Generic;
var weapon:int = 0;
var weapons:List.<GameObject>;
function Start () {
if(weapons.Count == 0) return;
for(var weap:GameObject in weapons){
weap.SetActive(false);
}
weapons[weapon].SetActive(true);
}
function Update () {
if(weapons.Count == 0) return;
var lastWeapon:int = weapon;
if(Input.GetKeyDown(KeyCode.Alpha1)) weapon = 0;
if(Input.GetKeyDown(KeyCode.Alpha2)) weapon = 1;
if(lastWeapon != weapon)Start();
weapons[weapon].GetComponent(LobWeapon).doUpdate();
}
As you can see, I forced the current weapon in the end to Update. That is not ideal. I worked around it pretty easy, but it still seems like a pain.
your script works correctly for me without forcing the update. I don’t see any issues in your code.
The only thing I can think of is you need to make sure the parent of the weapons is active.
You could just change your start function to the following:
function Start () {
if(weapons.Count == 0) return;
for(var weap:GameObject in weapons){
weap.SetActive(false);
}
if(!weapons[weapon].transform.parent.gameObject.activeSelf){
weapons[weapon].transform.parent.gameObject.SetActive(true);
}
weapons[weapon].SetActive(true);
}
Just checks if the weapons parent is active, if it isn’t it activates it. You could do this a lot of other ways depending on how your project is setup.
That’s my guess, as well. Unity’s update logic adheres to the Transform parent->child hierarchy. Even if the game object is active (i,e. activeSelf == true), its component’s Update() methods won’t be called unless all of its ancestors are also active.
C#
public void SetAllActive(GameObject go, bool state) {
go.SetActive(state);
if (go.transform.parent != null)
SetAllActive(go.transform.parent.gameObject, state);
}
I already verified that stuff. It was the first thing I checked. I don’t actually have that much of a problem with it, I just thought it odd that they stopped Updating. All ancestors and the current game object were active. (Just for note, the forced update also would not work if they were not.
)
Again, I am not complaining, I just thought it may be of worth to the admins or whom ever looks into these things.