C# script unity 2d creating a clone Error CS0165

96 Views Asked by At

here is my code below the problem i think is the if statement below 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

There are 1 best solutions below

0
On

in Unity3d when you create a GameObject like this

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!! of course you can get conflict from that change the second GameObject name. i think you need to learn more about Instantiate function here is a description for it.