- Home /
Switch Weapon Script Not taking effect HELP!!
Guys i trying to make a script of switch of Weapon, but not are taking effect :/ Here the script:
public var PrimaryWeapon : GameObject;
public var SecondaryWeapon : GameObject;
function Update () {
//if(Input.GetAxis ("Mouse ScrollWheel") > 0){ //Frente
if(Input.GetKeyDown ("1")){
PrimaryWeapon.active = true;
SecondaryWeapon.active = false;
}
//if(Input.GetAxis ("Mouse ScrollWheel") < 0){ //Tras
if(Input.GetKeyDown ("2")){
SecondaryWeapon.active = true;
PrimaryWeapon.active = false;
}
}
What i change for the script work? :/ thanks.
.enabled is deprecated and shouldn't be used any more, active is fine but I think (as I wrote in my answer, but it still has to be approved by a moderator here) that his weapons are not single gameobjects and are made of children so he should use PrimaryWeapon.SetActiveRecursively() ins$$anonymous$$d of just setting active on the root objects
Also when writing in JavaScript there is no need to use public variables. That is only C#(I don't know about Boo).
Answer by SkaredCreations · Nov 23, 2011 at 05:10 PM
I suppose that in Hierarchy you dragged the correct GameObjects to those variables (that is the GOs that you have inside the scene, and not a plain prefab from your Project), so it could be possible that your weapons aren't a single gameObject but contain children for the meshes/bones, so you can try to use SetActiveRecursively() instead of setting active only on the root objects (because of course it only acts on the root object itself and not on its children).
Answer by Mox.du · Nov 23, 2011 at 05:29 PM
This seems to be a bug. You probably attached your script to the camera. Attach it to some game object and it will work.
Implying that the camera isn't a gameObject? :)
Nah, just pulling your leg mate ;)
Don`t put it on weapon that you use active false. $$anonymous$$ake New Game Object and put it there.