- Home /
Error in switching weapons (activate/deactivate objects)
So im using the code below to switch weapons, but my third weapon does not appear when I press 3. I mean I have sound tracks that are children of my third object but they don't seem to work to. So its obviously not transformation problem.
var Primary : GameObject;
var Secondary : GameObject;
var Third : GameObject;
function Start ()
{
Primary.SetActiveRecursively(true);
Secondary.SetActiveRecursively(false);
Third.SetActiveRecursively(false);
}
function Update ()
{
if(Input.GetKeyDown("1")){
Primary.SetActiveRecursively(true);
Secondary.SetActiveRecursively(false);
Third.SetActiveRecursively(false);
}
if(Input.GetKeyDown("2" )){
Primary.SetActiveRecursively(false);
Secondary.SetActiveRecursively(true);
Third.SetActiveRecursively(false);
}
}
if(Input.GetKeyDown("3" )){
Primary.SetActiveRecursively(false);
Secondary.SetActiveRecursively(true);
Third.SetActiveRecursively(true);
}
}
Answer by Seth-Bergman · Jul 20, 2012 at 02:24 AM
if(Input.GetKeyDown("3" )){
Primary.SetActiveRecursively(false);
Secondary.SetActiveRecursively(true); // true
Third.SetActiveRecursively(true); //also true
}
need to set the second one to false
EDIT: to be sure, keep an eye on the game object in the inspector. Be sure "Maximize on play" is unchecked, and hit play. Select the third weapon in the hierarchy panel. if the checkbox is checked when you hit 3, you know the code is working.. In which case hit "pause" and hover over the scene view, and hit f to frame...
if the object is active, the box will be checked, like this:
I mean, when you hit 2, the second weapon comes up, right? Then, if you hit 3, do you just get no weapon at all, or you still have the other?
Ok, so when I start, I have the first weapon visible. I press 2 and it appears. I press 3. It doesn't. So I could actually cycle through them all but the last one (3) does not appear what so ever. I tried everything.
Your answer
![](https://koobas.hobune.stream/wayback/20220613074432im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
Switching Between Weapons? 1 Answer
shooting script error (java) 2 Answers
Some problems after switch the game for iOS mode 2 Answers
Exception error 1 Answer
Is not a Member of Collisions 2 Answers