- Home /
Referencing a script on the same object comes back with an error
I am getting "NullReferenceException: Object reference not set to an instance of an object" even though the script I am referencing is on the same Object, strangely I got another script on another object to work, I used the same code and modified it for a different use, one I thought should be simpler since both scripts are on the same Object.
This is what doesnt work
public class CrosshairGUI : MonoBehaviour
{
private CrosshairRay rayscript;
private Color curcol;
void Awake()
{
rayscript = GetComponent<CrosshairRay>();
}
void OnGUI()
{
GUI.color = curcol;
GUI.Label (new Rect(Screen.width/2,Screen.height/2, 50, 50), "+");
}
void Update()
{
if(rayscript.imnear)
{
curcol = Color.red;
}
else
{
curcol = Color.white;
}
}
}
and the one that does
public class Muteaudioclip : MonoBehaviour
{
public GameObject player;
private CrosshairRay rayscript;
void Awake()
{
rayscript = player.GetComponent<CrosshairRay>();
}
void OnMouseDown()
{
if(rayscript.imnear)
{
if(audio.mute == false)
{
audio.mute = true;
Debug.Log ("Sound Off");
}
else
{
audio.mute = false;
Debug.Log ("Sound On");
}
}
}
}
Im guessing its something simple Ive overlooked.
I'm not sure if this is it but you made your player GameObject public while the rayscript variable is private, try making the script variable public as well and I think that should work ...
Eugenius > I doubt that's it. The public player is in the script thats working. Its the rayscript variable thats giving him the error, which is private in both cases.
@Em3rgency, that's what I suggested, that the rayscript var is the actual problem :).
private or public it doesnt seem to make a difference "NullReferenceException: Object reference not set to an instance of an object CrosshairGUI.Update () (at Assets/$$anonymous$$ain Project/Scripts/CrosshairGUI.cs:25)" still showing.
Im guessing the :25 means line 25 which is "if(rayscript.imnear)" but again, it works fine on the other script which is on a different object from the CrosshairRay script Im referencing.
@henrypuspurs could you please share the piece of code from the rayscript with the imnear variable?
Answer by henrypuspurs · Jul 19, 2013 at 12:10 PM
Combined GUI Crosshair and Raycast script works fine attached to the camera, in case anyone wants the solution here it is.
using UnityEngine;
using System.Collections;
public class Crosshair : MonoBehaviour
{
private Color curcol;
public float interactionDistance = 3f;
public bool imnear;
void Start ()
{
Screen.lockCursor = true;
}
void OnGUI()
{
GUI.color = curcol;
GUI.Label (new Rect(Screen.width/2,Screen.height/2, 50, 50), "+");
}
void Update()
{
RaycastHit hit;
Debug.DrawRay(transform.position, transform.forward * interactionDistance);
if(Physics.Raycast (transform.position, transform.forward, out hit, interactionDistance))
{
if(hit.collider.tag == "Clickable")
{
imnear = true;
}
else
{
imnear = false;
}
}
else
{
imnear =false;
}
if(imnear == true)
{
curcol = Color.green;
}
else
{
curcol = Color.white;
}
}
}
And an example of how that script is referred to from another object.
using UnityEngine;
using System.Collections;
public class Audiomute : MonoBehaviour
{
private GameObject player;
private Crosshair looking;
void Awake()
{
GameObject player = GameObject.Find("Player/Camera");
looking = player.GetComponent<Crosshair>();
}
void OnMouseDown()
{
if(looking.imnear)
{
if(audio.mute == false)
{
audio.mute = true;
//maybe change colour of halo
Debug.Log ("Sound Off");
}
else
{
audio.mute = false;
//maybe change colour of halo
Debug.Log ("Sound On");
}
}
}
}
Answer by Eugenius · Jun 24, 2013 at 02:47 PM
Instead of:
if(Physics.Raycast (transform.position, transform.forward, outhit, interactionDistance))
{
imnear = (hit.collider.tag == "Clickable");
}
Try to do this:
if(Physics.Raycast (transform.position, transform.forward, out hit, interactionDistance))
{
if(hit.collider.tag == "Clickable"){
imnear = true;
}
}
Still no luck, this is getting really frustrating, it should work, but refuses to.
O$$anonymous$$ done some more testing, now the script does everything I need it to, but the error still comes up, which is a pain and may cause issues down the line.
$$anonymous$$aybe add an if(rayscript != null) ? That's the last thing I can think of...
I don't quite understand, where on what script would I put that? (you might have guessed by now Im completely new to coding)
void Update()
{
if(rayscript != null)
{
if(rayscript.imnear)
{
curcol = Color.red;
}
else
{
curcol = Color.white;
}
}
}
Answer by SpecticalPro · Jun 24, 2013 at 06:09 PM
try this:
public class CrosshairGUI : MonoBehaviour {
private CrosshairRay rayscript;
private Color curcol;
void Awake()
{
rayscript = GetComponent<CrosshairRay>();
}
void OnGUI()
{
GUI.color = curcol;
GUI.Label (new Rect(Screen.width/2,Screen.height/2, 50, 50), "+");
}
void Update()
{
if(rayscript!=null){
if(rayscript.imnear)
{
curcol = Color.red;
}
else
{
curcol = Color.white;
}
}else{
rayscript = gameObject.getComponent<CrosshairRay>();
}
} }
Sorry for the bad tabbing
sorry, entered it an hour ago, guess it took this long to go through
Answer by SpecticalPro · Jun 24, 2013 at 06:08 PM
try wrapping the if/ else if statement in your update with a null check for the rayscript, if its null set it to gameObject.getComponent();
Could you give me an example of how to do that? I hate to ask for a favour like that but I have no coding experience so pretty much everything is new to me and Im learning as I go (as I need a script, I try and learn how to do it).
Your answer
Follow this Question
Related Questions
Distribute terrain in zones 3 Answers
Multiple Cars not working 1 Answer
Elements in draggable window do not remain fixed [SOLVED] 2 Answers
Create Multiple Foldouts. 1 Answer