Object reference not set to an instance of an Object C#
Hi guys! I need help with this thing that driving me nuts.
There are two scripts, one attached to ballPrefeb called "clickToDestroy.cs" and "activateAnimation.cs" that is attached to character who is going to be animated. The thing that I want is that when the ball is destroyed (clicked by OnMouseDown) it reads the position where has been destroyed and use that in "activateAnimation.cs" script for "triggering" particular animation based on that position. Problem (for now) is that I'm getting an error "NullReferenceException: Object reference not set to an instance of an object" in line 28. I assume that I'm getting this error because script doesn't know for what object are those positions. Please help me with this one! Here are the scripts.
This is "clickToDestroy.cs":
using UnityEngine;
using System.Collections;
public class clickToDestroy : MonoBehaviour {
public GameObject splashReference;
public GameObject effectReference;
public int scoreValue;
private GameController gameController;
public Vector3 _lastPosition;
void Start()
{
GameObject gameControllerObject = GameObject.FindWithTag("GameController");
if (gameControllerObject != null)
{
gameController = gameControllerObject.GetComponent<GameController>();
}
if (gameController == null)
{
Debug.Log("Cannot find 'GameController' script");
}
}
public void OnMouseDown ()
{
gameController.AddScore(scoreValue);
_lastPosition = new Vector3(transform.position.x, transform.position.y, transform.position.z);
Destroy(gameObject);
Instantiate(splashReference, transform.position, transform.rotation);
Instantiate(effectReference, transform.position, transform.rotation);
Debug.Log(_lastPosition);
}
}
And this is "activateAnimation.cs":
using UnityEngine;
using System.Collections;
public class activateAnimation : MonoBehaviour {
public Animator anim;
public clickToDestroy _clickToDestroy;
// GameObject yourObjects;
// private GameObject theCollision;
public void Start()
{
anim = GetComponent<Animator>();
_clickToDestroy = gameObject.GetComponent<clickToDestroy>();
}
public void Update()
{
if (_clickToDestroy._lastPosition.x >= 10 && _clickToDestroy._lastPosition.x <= 80)
{
anim.SetTrigger("levi");
}
}
}
Answer by Tyche10 · May 11, 2016 at 10:46 AM
As far as I can see both your scripts are attached to the ball that you are destroying. When a gameobject is destroyed, all scripts attached to it are destroyed as well. So if you want a script to outlive a gameobject you should attach it to another gameobject (for example an empty gameobject called Managers or something).
Answer by stevendale · May 11, 2016 at 11:04 AM
Thank you for your answer @Tyche10 !
I have GameController script and from there balls are getting generated as clones. So those clones are destroying script as well, even if i attached script to ballPrefeb? And if I try solution to make new gameobject, should I attach just "clickToDestroy.cs" to it?
A monobehaviour script only works if there is an instance of it in the scene, in other words: if there is a gameobject in the scene that has the script attached to it, there is an instance of the script(component) in the scene. So you will only have an instance of the script attached to a prefab if there is an instance (or clone) of the prefab in the scene.
I would leave the clickToDestroy script on the ball, but move the _lastPosition variable to the gameController script. Then, before you make the ball destroy itself, update the _lastPosition value in the gameController script. The activateAnimation script should be attached to something other than the ball because you want it to still function after the ball is destroyed. The activateAnimation script then can get the necessary information about _lastPosition from the gameController script which also still exists after the ball has been destroyed (if it is attached to something other than the ball of course).
Oh, thank you, I didn't know that about monobehaviour, I'm still new at this.
I like that idea to "save" _lastPosition variable in gameController script but could you please help me how to do it?
Your gameController script should just declare a member variable:
public class gameController: $$anonymous$$onoBehaviour {
public Vector3 _lastPosition;
...
}
And since you already keep a reference to the gameController in your clickToDestroy script, you can do:
public void On$$anonymous$$ouseDown ()
{
gameController._lastPosition = new Vector3(transform.position.x, transform.position.y, transform.position.z);
Destroy(gameObject);
}
ins$$anonymous$$d of
_lastPosition = new Vector3(transform.position.x, transform.position.y, transform.position.z);
I wrote what you said and change in activateAnimation.cs:
using UnityEngine;
using System.Collections;
public class AktiiranjeAnimacije : $$anonymous$$onoBehaviour {
public Animator anim;
public GameController _clickToDestroy;
// GameObject yourObjects;
// private GameObject theCollision;
public void Start()
{
anim = GetComponent<Animator>();
_clickToDestroy = GetComponent<GameController>();
}
public void Update()
{
if (_clickToDestroy._lastPosition.x >= 10 && _clickToDestroy._lastPosition.x <= 80)
{
anim.SetTrigger("levi");
}
}
}
But I still getting "NullReferenceException: Object reference not set to an instance of an object" in line 28.
First I would change the name of _clickToDestroy in your AktiiranjeAnimacije script to gameController for clarity, and make it private ins$$anonymous$$d of public so it doesn't interfere with your gameController variable in your _clickToDestroy script (I don't think they will necessarily interfere, since they can be recognized according to the class they're part of, but it's good practice to be sure). Give clear, unconfusing names to your variables and keep them as private as possible (also your functions are better off private if you don't need to access them from other scripts). Trust me, it will make things a lot easier for you down the road if you make that a habit ;)
Does he throw the null reference in line 28 in your activateAnimation script (is that script also still present?) or does he throw it in line 28 of your new AktiiranjeAnimacije script?
First, I really appreciated your help, thank you! :)
It gives an error "_GameController is null. This is the problem"! And I've done thing for frame, I read something about that earlier but I'm so out of my head with this one. Also thank you for that help! :)
No problem ;) So did you solve it now? Apparently the gameobject to which you attached AktiiranjeAnimacije script, doesn't have a GameController script attached to it, so when you call GetComponent(); it returns null. So all you should do is add a GameController script to the same object as the AktiiranjeAnimacije script