- Home /
Using weapon switching script makes any weapons with animations disappear
EDIT: Have discovered that if I remove the animation from my sword, it appears. What good is that though, having a weapon I can't use, unless this is jousting?
Ok, so my original question asked about getting a weapon switching script working, which I've now done, praise God! =) There's a strange issue now though, and thought I'd throw it out to the community. If I follow exactly as the tutorial here shows, add the script (below), parent script to main camera, child an empty object to the main camera (a weapon manager, so to speak) and child a cube (weapon) and sphere (another weapon) to that manager, everything goes swimmingly, I can switch between the cube and sphere. Yay! Accept, nobody wants to carry a cube or sphere around as a weapon. No problem, right? That's just proof of concept. I'll just add my sword on there, which I've been using with no issues for the longest... wait, dude, where's my sword? It's like, not there! I mean, I did check to make sure there were slots for three weapons, and nothing. Cube and sphere are there just fine, but my sword (a 3d model) doesn't appear. Once again, any help or advice is humbly accepted and appreciated. God bless.
function Start () {
// Select the first weapon
SelectWeapon(0);
}
function Update () {
// Did the user press fire?
if (Input.GetButton ("Fire1"))
BroadcastMessage("Fire");
if (Input.GetKeyDown("1")) {
SelectWeapon(0);
}
else if (Input.GetKeyDown("2")) {
SelectWeapon(1);
}
else if (Input.GetKeyDown("3")) {
SelectWeapon(2);
}
}
function SelectWeapon (index : int) {
for (var i=0;i<transform.childCount;i++) {
// Activate the selected weapon
if (i == index)
transform.GetChild(i).gameObject.SetActiveRecursively(true);
// Deactivate all other weapons
else
transform.GetChild(i).gameObject.SetActiveRecursively(false);
}
}
Answer by YoungDeveloper · Sep 13, 2013 at 05:57 PM
Instead of finding child (weapon) in for loop, I'd suggest creating an GameObject or whatever array for weapons, lets say size of 5 (5 weapons slots). Then set each object in he inspector, to access that object you just write the index, array[0]...
Answer by Conect11 · Sep 13, 2013 at 10:34 PM
Wahoo, I love jury - rigged solutions! Ok, the quick and dirty I way I found of fixing this is to attach another game object (say, a cube) into the weapon select game object, position it so that it can't be seen, and then parent the sword (or whatever weapon you want) to that. Then you can have your animation on the weapon (not the new game object) and everything works great. =)
Your answer
Follow this Question
Related Questions
Mecanim not updating script when it switches state? 2 Answers
weapon animation question 1 Answer
Attaching Weapon to Animated Character 0 Answers
Multiple Weapon Animation Help 0 Answers
can some onehelp me with this code(Js). (No Errors). 1 Answer