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