C#脚本统一2d创建克隆错误CS0165

时间:2015-06-21 03:08:10

标签: c# unity3d unityscript

这里是我的代码下面的问题我认为是if语句下面的input.getaxis vertical

using UnityEngine;
using System.Collections;

public class moveit : MonoBehaviour {

    private Collectable shoot;
    private static int ammun;
    public  static bool defense;
    public arrow Arrow;
    public float Speed = 1f;
    private float movex = 0f;
    private float movey = 0f;
    public static Animator animator;
    GameObject projectile = null;



    // Use this for initialization
    void Start () {
        animator = GetComponent<Animator> ();
        shoot =  GetComponent<Collectable>();       
    }

    // Update is called once per frame
    void Update () {
        ammun = Collectable.ammo;
    //  the movement and aanimation code----------------------
        movex = Input.GetAxis ("Horizontal");
        if (movex == 0f){
        animator.SetInteger ("AnimState", 1);
        }else if(movex == 1f){
        animator.SetInteger ("AnimState", 2);
        }else if(movex == -1f){
        animator.SetInteger ("AnimState", 3);
        }
        movey = Input.GetAxis ("Vertical");
        if(ammun == 1 && movey == -1){
            GameObject projectile = Instantiate (projectile, new Vector3 (0, 0, 0), Quaternion.identity) as GameObject;
        }

        rigidbody2D.velocity = new  Vector2 (movex * Speed, movey * Speed); }
    //-----------------------------------------------------------------------------
}

1 个答案:

答案 0 :(得分:0)

当您创建像这样的GameObject时,在Unity3d中

GameObject projectile = null;  

你不能将它分配给编辑器中的任何内容,因为你的GameObject保持为null,当你实例化它时会得到错误。请改用此代码

public GameObject projectile = null;  

现在您必须在编辑器中将此GameObject分配给您想要实例化的内容。

<强>更新
我注意到你立即将一个null GameObject放入一个具有确切名称的GameObject中!当然你可以通过改变第二个GameObject名称来解决冲突。 我认为你需要了解更多关于实例化函数here的描述。