Can't access a method from another script (Object reference not set to an instance of an object)
Hello community!
I have a script Called Projectile where I have some code in order to be able to do object pooling in it and in my Character Controller Script I have set a quick test 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?
public class Projectile : MonoBehaviour
{
public Transform target;
public float moveSpeed = 100f; // speed at which the projtive moves towards the target
public float rotationSpeed = 10f; // speed at which the project rotates towards the target
public float shootTime = 1f;
public bool updateRotation = false; // whether or not the projectile should continue to update its rotation after it was spawned
public int pooledAmount = 3; // max amount of arrows that will be able to be shoot at once
public GameObject arrow;
public List<GameObject> arrows; // create a list containing all the arrows that will be allocated to memory once it starts
void Start()
{
transform.LookAt(target); // set the default direction of the projectile to face towards the target
arrows = new List<GameObject>();
for (int i = 0; i < pooledAmount; i++)
{
GameObject obj = (GameObject)Instantiate(arrow);
obj.SetActive(false);
arrows.Add(obj);
}
}
void Update()
{
Rotate();
Move();
}
/// <summary>
/// Rotates the projectile towards the target.
/// </summary>
void Rotate()
{
Vector3 targetPosition = target.position - transform.position;
float speed = rotationSpeed * Time.deltaTime;
Vector3 targetDirection = Vector3.RotateTowards(transform.forward, targetPosition, speed, 0);
transform.rotation = Quaternion.LookRotation(targetDirection);
}
/// <summary>
/// Move the projectile towards the target.
/// </summary>
void Move()
{
transform.position = Vector3.MoveTowards(transform.position, target.position, moveSpeed * Time.deltaTime);
}
/// <summary>
/// "Destorys" the object
/// </summary>
void OnEnable ()
{
Invoke("Destroy", 2f);
}
void Destory ()
{
gameObject.SetActive(false);
}
void OnDisable ()
{
CancelInvoke();
}
public void Fire()
{
for (int i = 0; i < arrows.Count; i++) // loops through the list
{
if (!arrows[i].activeInHierarchy) // if the arrow is not currently active (looking for inactive arrows)
{
arrows[i].transform.position = transform.position;
arrows[i].transform.rotation = Quaternion.identity;
arrows[i].SetActive(true);
break;
}
}
}
}
Script where I try to call the method FIRE from the previous one:
public class Player_ClickToMove : MonoBehaviour
{
public Projectile projectileScript;
public GameObject target;
public GameObject arrowTest;
public Transform arrowTargetTest;
void Awake ()
{
projectileScript = GetComponent<Projectile>();
}
if (Input.GetKeyDown(KeyCode.Z))
{
projectileScript.GetComponent<Projectile>().Fire();
Debug.Log("Shooted");
}
}
}
A) you should mention the line that throws the error ins$$anonymous$$d of making us read all 100 lines and guess. The line number should be mentioned in the error message.
B) There's no need to get the Projectile script from your Projectile script projectileScript.GetComponent<Projectile>().Fire();
. What you do in Awake should be enough. You are already GetComponent()ing said component in there.
C) based on the na$$anonymous$$g I would guess your Player_ClickTo$$anonymous$$ove
script is attached to the player object? If it is, you are trying to get a Projectile
script attached to your player object. Is there a Projectile
script attached to the same object as the Player_ClickTo$$anonymous$$ove
script? If there isn't, projectileScript.GetComponent<Projectile>()
will return null and calling Fire()
on a null Unity.Object reference will cause a the error you are getting.
Answer by henmachuca · Oct 27, 2016 at 04:35 PM
Indeed I managed to find my way around it with your tips.
Thank you @NoseKills, and sorry for not showing the code line with the error. I'll do that for my next questions.
Cheers.
Your answer
Follow this Question
Related Questions
[Help] how do I reference/access another script in unity C# 2 Answers
Creating an object using a prefab, with parent, text etc.. 1 Answer
Basic Unity: Can someone explain the referencing and instantiation process? 1 Answer
IndexOutOfRangeException: Array index is out of range. 2 Answers
Help for Quest System 2 Answers