Search Unity

Object Reference problem - Calling method from another script

Discussion in 'Scripting' started by henmachuca, Oct 26, 2016.

  1. henmachuca

    henmachuca

    Joined:
    Oct 14, 2016
    Posts:
    105
    Hello community!

    I have a script Called **Projectile** where I have some code in order to be able to do object pooling from it and in my **Character Controller Script** I have set a quick test when I press Z to try things out and see if the calling method works but I get the object reference not set and I can't figure my way out of it.

    Can anyone help me out please?


    Code (CSharp):
    1.     public class Projectile : MonoBehaviour
    2.     {
    3.         public Transform target;
    4.  
    5.         public float moveSpeed = 100f;          // speed at which the projtive moves towards the target
    6.         public float rotationSpeed = 10f;      // speed at which the project rotates towards the target
    7.         public float shootTime = 1f;
    8.  
    9.         public bool updateRotation = false;     // whether or not the projectile should continue to update its rotation after it was spawned
    10.         public int pooledAmount = 3;         // max amount of arrows that will be able to be shoot at once
    11.  
    12.         public GameObject arrow;
    13.         public List<GameObject> arrows;            // create a list containing all the arrows that will be allocated to memory once it starts
    14.  
    15.         void Start()
    16.         {
    17.             transform.LookAt(target);   // set the default direction of the projectile to face towards the target
    18.             arrows = new List<GameObject>();
    19.             for (int i = 0; i < pooledAmount; i++)
    20.             {
    21.                 GameObject obj = (GameObject)Instantiate(arrow);
    22.                 obj.SetActive(false);
    23.                 arrows.Add(obj);
    24.             }
    25.         }
    26.  
    27.         void Update()
    28.         {
    29.             Rotate();
    30.             Move();
    31.         }
    32.  
    33.         /// <summary>
    34.         /// Rotates the projectile towards the target.
    35.         /// </summary>
    36.         void Rotate()
    37.         {
    38.             Vector3 targetPosition = target.position - transform.position;
    39.  
    40.             float speed = rotationSpeed * Time.deltaTime;
    41.  
    42.             Vector3 targetDirection = Vector3.RotateTowards(transform.forward, targetPosition, speed, 0);
    43.  
    44.             transform.rotation = Quaternion.LookRotation(targetDirection);
    45.         }
    46.  
    47.         /// <summary>
    48.         /// Move the projectile towards the target.
    49.         /// </summary>
    50.         void Move()
    51.         {
    52.             transform.position = Vector3.MoveTowards(transform.position, target.position, moveSpeed * Time.deltaTime);
    53.         }
    54.  
    55.         /// <summary>
    56.         /// "Destorys" the object
    57.         /// </summary>
    58.         void OnEnable ()
    59.         {
    60.             Invoke("Destroy", 2f);
    61.         }
    62.         void Destory ()
    63.         {
    64.             gameObject.SetActive(false);
    65.         }
    66.         void OnDisable ()
    67.         {
    68.             CancelInvoke();
    69.         }
    70.  
    71.         public void Fire()
    72.         {
    73.             for (int i = 0; i < arrows.Count; i++)     // loops through the list
    74.             {
    75.                 if (!arrows[i].activeInHierarchy)      // if the arrow is not currently active (looking for inactive arrows)
    76.                 {
    77.                     arrows[i].transform.position = transform.position;
    78.                     arrows[i].transform.rotation = Quaternion.identity;
    79.                     arrows[i].SetActive(true);
    80.                     break;
    81.                 }
    82.             }
    83.         }
    84.     }
    Script where I try to call the method FIRE from the previous one:

    Code (CSharp):
    1. public class Player_ClickToMove : MonoBehaviour
    2.     {
    3.         public Projectile projectileScript;
    4.  
    5.         public GameObject target;
    6.  
    7.         public GameObject arrowTest;
    8.         public Transform arrowTargetTest;
    9.  
    10.         void Awake ()
    11.         {
    12.             projectileScript = GetComponent<Projectile>();
    13.         }
    14.  
    15.                 if (Input.GetKeyDown(KeyCode.Z))
    16.                 {
    17.                     projectileScript.GetComponent<Projectile>().Fire();
    18.                     Debug.Log("Shooted");
    19.                 }
    20.             }
    21.         }
     
  2. zombiegorilla

    zombiegorilla

    Moderator

    Joined:
    May 8, 2012
    Posts:
    9,051
    You are calling fire on awake. Meaning it is attempting to run before the start method is called, so arrows hasn't been initialized yet, so it is null.
     
  3. henmachuca

    henmachuca

    Joined:
    Oct 14, 2016
    Posts:
    105