如何在C#中使用实例化?

How to use instantiation in C#?

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 当我按 space 实例化时,它首先实例化 1,然后实例化 2,然后实例化 4,然后实例化 8,依此类推 - 为什么它不在每次按下时实例化 1。 我正在尝试在这里制造一把枪,显然需要它一次只能发射一颗子弹,因为此刻它会发射多颗子弹。正如我已经解释过的,它会先发射一颗子弹,然后下次我发射时它会加倍发射,依此类推。

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.

如果行为是每次按下键时每个预制件都会复制,则仅表示脚本也附加到预制件或多个游戏对象。我每次都看到类似的问题。

如果脚本附加到任何预制件,请从预制件中删除脚本。从场景中的每个 GameObject 中删除它,然后仅将其附加到 one 个 GameObject。

Select 脚本,转到 Assets --> Find References in Scene。它将向您显示每个附加了此脚本的游戏对象。除了一个之外,将它从所有的人中删除。