简体   繁体   English

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

[英]C# script unity 2d creating a clone Error CS0165

here is my code below the problem i think is the if statement below input.getaxis vertical这是我在问题下方的代码,我认为是 input.getaxis vertical 下面的 if 语句

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); }
    //-----------------------------------------------------------------------------
}

in Unity3d when you create a GameObject like this在 Unity3d 中创建这样的 GameObject 时

GameObject projectile = null;  

you can't assign it to anything in editor and there for because your GameObject remains null your getting error when you instantiate it.你不能将它分配给编辑器中的任何东西,因为你的游戏对象在你实例化它时仍然为空你得到错误。 use this code instead改用此代码

public GameObject projectile = null;  

now you must assign this GameObject in the Editor to what ever you want to instantiate.现在您必须将编辑器中的这个游戏对象分配给您想要实例化的内容。

UPDATE更新
and i noticed you instantate a null GameObject and put it in a GameObject with the Exact name!!我注意到你实例化了一个空的 GameObject 并将它放在一个具有确切名称的 GameObject 中!! of course you can get conflict from that change the second GameObject name.当然,您可以从更改第二个游戏对象名称中获得冲突。 i think you need to learn more about Instantiate functionhere is a description for it.我认为你需要了解更多关于 Instantiate 函数的信息,这里是它的描述。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 四元数的结果上的C#CS0165 - C# CS0165 on the result of Quaternion C#错误CS0165:使用未分配的局部变量 - 忽略逻辑和输出引用 - C# error CS0165: Use of unassigned local variable - ignoring logic and out reference 对于未分配的局部变量,是否保证CS0165 C#编译器错误? - Is CS0165 C# compiler error guaranteed for unassigned local variables? 继续收到错误代码CS0165 - Keep receiving error code CS0165 错误 CS0165:使用未分配的局部变量“sot” - error CS0165: Use of unassigned local variable 'sot' 错误 CS0165 使用未分配的局部变量“json” - Error CS0165 Use of unassigned local variable 'json' 谁能知道我出了什么问题,因为我是统一错误 CS0165 的新手:使用未分配的局部变量 'diff' - can anyone what I'm going wrong as I'm new in unity error CS0165: Use of unassigned local variable 'diff' 为什么编译器抛出错误CS0165:使用未分配的局部变量? - Why compiler throw error CS0165: Use of unassigned local variable? 如何修复 XmlReader Switch Case 中的“错误 CS0165:使用未分配的局部变量” - How to fix 'error CS0165: Use of unassigned local variable' in XmlReader Switch Case 如何修复错误CS0165:“使用未分配的局部变量&#39;a&#39;”? - How do I fix Error CS0165: “Use of unassigned local variable 'a'”?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM