- Home /
Question by
ljm35 · Oct 30, 2015 at 09:14 PM ·
script error
hello i get an error when i'm trying to acces another variable in a srcipt that is attached to another game object and the error says "NullReferenceException:Object reference not set to an instance of an object" (thankyou in advance :)
using UnityEngine;
using System.Collections;
public class celleat : MonoBehaviour {
public GameObject targetObject;
public CameraFollow scriptWantToAcces;
private float Mass = 1f;
// Use this for initialization
void Start () {
scriptWantToAcces = GameObject.FindGameObjectWithTag("MainCamera").GetComponent< CameraFollow > ();
transform.localScale = new Vector3 (Mass,1,Mass);
if (scriptWantToAcces = null) {
Debug.Log ("No Script");
} else {
Debug.Log ("found Script"); //it says this in the debug
}
}
void Update (){
}
void OnTriggerEnter(Collider other) {
if (other.tag == "eatebal") {
Destroy (other.gameObject);
Mass += 0.1f;
transform.localScale = new Vector3 (Mass,1,Mass);
scriptWantToAcces.offset += 0.1f; // here i get the NullReferenceException:Object reference not set to an instance of an object
}
}
}
heres the script im trying to acces : public class CameraFollow : MonoBehaviour {
public float offset;
public GameObject target;
// Use this for initialization
void Start () {
}
// Update is called once per frame
void Update () {
Vector3 camTrans = new Vector3 (target.transform.position.x, transform.position.y + offset, target.transform.position.z);
transform.position = camTrans;
}
}
Comment
Answer by gjf · Oct 30, 2015 at 06:16 PM
you don't state which line the error is on, but this line:
if (scriptWantToAcces = null) {
should read
if (scriptWantToAcces == null) {
for safety, any time you use a reference to the script/component, you should check for null.