- Home /
My Grenade isnt working, help!
Hey everyone, im new to Unity and I've been slowly learning how to use it and java through tutorials, but I've hit a snag. I have a script that's suppose to make me throw a grenade. It is suppose to make my gun disappear, when i click the left mouse button throw the grenade, (Iv'e made the animation) then throw a prefab sphere that i created, but all it does is layer on top of my gun and do nothing. I've checked with the Scripter i followed and he said the script is fine, but maybe one of you can help me? Ill add the two scripts im using for it below, maybe its not in the scripts that its not working? Anyway please help!!
My Choose Equipment Script -
//First Gun, AR,SMG,Ect var Primary : GameObject; //Second Gun, Pistol Ect var Secondary : GameObject;
var GrenadeObj : GameObject;
// Use this for initialization function Start () { Secondary.SetActiveRecursively(false); Primary.SetActiveRecursively(true); GrenadeObj.SetActiveRecursively(false); }
// Update is called once per frame function Update () { if(Input.GetKeyDown("1") && Secondary.active == true){ Primary.SetActiveRecursively(true); Secondary.SetActiveRecursively(false); }
if(Input.GetKeyDown("2") && Primary.active == true){
Primary.SetActiveRecursively(false);
Secondary.SetActiveRecursively(true);
}
if(Input.GetButtonDown("Grenade"))
{
GrenadeObj.SetActiveRecursively(true);
}
}
My Throw Grenade Script -
var Grenade : Rigidbody; var Throw : String; var Spawn : Transform;
private var _MainCam : GameObject;
// Use this for initialization function Start () { _MainCam = GameObject.FindGameObjectWithTag("WeapCam");
}
// Update is called once per frame function Update () { if(Input.GetButtonDown("Grenade")) { gameObject.animation.Play(Throw); gameObject.animation[Throw].speed =1.5; _MainCam.GetComponent(ChooseEquipment).Primary.SetActiveRecursively(false); _MainCam.GetComponent(ChooseEquipment).Secondary.SetActiveRecursively(false);
Toss();
}
}
function Toss(){ yield WaitForSeconds(1); gren = Instantiate(Grenade,Spawn.position,Spawn.rotation); Grenade.rigidbody.AddForce(transform.forward * 1000); gameObject.SetActiveRecursively(false); _MainCam.GetComponent(ChooseEquipment).Primary.SetActiveRecursively(true); }
Please Help!
Answer by sparkzbarca · Dec 20, 2012 at 05:29 AM
IT SEEMS grenade should be a gameobject not a rigidbody
you have for example
var Grenade : Rigidbody
which should mean Grenade is a rigidbody
but then you later instantiate it.
Instantiate(Grenade,Spawn.position,Spawn.rotation)
then you access its rigidbody
Grenade.Rigidbody.
Which is wierd because while a rigidbody can access itself it makes no sense to do so.
You might as well have typed
Grenade.Rigidbody.Rigidbody.Rigidbody.Rigidbody
Its legal but nonsensical.
Your answer
Follow this Question
Related Questions
My animation is not importing into Unity. 0 Answers
Script not working 0 Answers
"GetQualityLevel" is not a member of "QualitySettings" 2 Answers
issues with collision detection 1 Answer
colliders not working 0 Answers