Instantiating two prefabs instead one how to fix it?
Well hello Guys back with another problem. Im making some grenade throw thing but instead of one grenade but two grenades.. How to fix that. Here's my script.
public class GrenadeThrow1 : MonoBehaviour {
public Rigidbody grenade;
public Rigidbody iceGrenade;
public float throwPower = 10;
public Button grenadeButton;
public Button iceGrenadeButton;
// Use this for initialization
void Start () {
grenadeButton.onClick.AddListener(Grenade);
iceGrenadeButton.onClick.AddListener(IceGrenade);
}
IEnumerator GrenadeCoolDown () {
yield return new WaitForSeconds(4);
grenadeButton.interactable = true;
}
IEnumerator IceGrenadeCoolDown () {
yield return new WaitForSeconds(4);
iceGrenadeButton.interactable = true;
}
void Grenade () {
var clone = GetComponent<Rigidbody>();
clone = Instantiate(grenade, transform.position, transform.rotation);
clone.velocity = transform.TransformDirection(Vector3.forward * throwPower);
grenadeButton.interactable = false;
StartCoroutine(GrenadeCoolDown());
Debug.Log("Grenade");
Debug.Log(GetInstanceID());
}
void IceGrenade () {
var clone1 = GetComponent<Rigidbody>();
clone1 = Instantiate(iceGrenade, transform.position, transform.rotation);
clone1.velocity = transform.TransformDirection(Vector3.forward * throwPower);
iceGrenadeButton.interactable = false;
StartCoroutine(IceGrenadeCoolDown());
}
}
So i try Debug.Log if it is calling twice. But yes it's calling twice. How to fix it? Please Help me..
Answer by MrCrumbl3d · May 24, 2017 at 07:13 AM
For those who stuck with this situation. Make sure your not cloning another gameobjects who handling with the script. And also make sure the player or object that who handle that script it must be 1 collider only not two colliders.
Answer by sandeepsmartest · May 24, 2017 at 06:31 AM
Check if the script "GrenadeThrow1.cs" is added twice.To know this simply right click on the script in project panel and click "Find References In Scene".Another way - to know on which object does this script attached, simply print("Attached to: "+this.gameobject.name) in start() of the above posted script.