using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class Gun : MonoBehaviour {
public int Player_Health = 100;
public GameObject Player;
public Rigidbody Bullet;
public Transform Guun;
public bool Player_Dead = false;
// Use this for initialization
void Start () {
}
// Update is called once per frame
void Update () {
if (Input.GetKeyDown(KeyCode.Space))
{
Rigidbody rocketInstance;
rocketInstance = Instantiate(Bullet, Guun.position, Guun.rotation) as Rigidbody;
rocketInstance.AddForce(Guun.forward * 5000);
}
if(Player_Health == 0)
{
Player_Dead = true;
}
if(Player_Dead == true)
{
Destroy(Player);
}
}
private void OnCollisionEnter(Collision col)
{
if(col.gameObject.name == "Player")
{
Player_Health = Player_Health - 20;
Debug.Log(Player_Health);
}
}
}
1.So when i instantiate by pressing space it first instantiates 1 then 2 then 4 then 8 and so on - why doesnt it just instantiate 1 each press.
If the behavior is that each prefab duplicates each time the key is pressed, it simply means that the script is also attached to the prefab or multiple GameObjects. I see similar problems each time.
Please remove the script from the prefabs if it is attached to any prefab. Remove it from every GameObject in the scene then attach it to one GameObject only.
Select the script, go to Assets --> Find References in Scene. It will show you every GameObject that has this script attached to it. Remove it from all of them except for one.