- Home /
UnassignedReferenceException: Assigning through find()
Oh, again i've got an error. I have a code:
Public class Movement : Monobehaviour {
public Transform Joystick;
Void Start() {
Joystick = GameObject.Find("Joystick").transform;
}
}
which works fine: script finds joystick, and joystick works perfect with moving character, but one thing is that in console i see like 999+ errors which says that i haven't assigned variable yet.
Notice that i used public not private because i need an access to it in other script.
Thanks, hope i'll get some help =)
In general, when you need access to something in another script, you'd use a Property. These give you greater control over how the objects are accessed (for example, you can stop another script from re-assigning the variable). The errors are not co$$anonymous$$g from that bit of code, though. If that bit of code were throwing an error, it would throw it once. Show the rest of the code, and the exact contents of the errors.
Ofcourse error comes from other script, where i compare that Transform in Update() function :)
Yes, but we can't help unless we can see that script, because there's nothing wrong with this as long as the GameObject "Joystick" exists in the scene. (Except for the fact that void
and public
shouldn't have capital letters, but since that wouldn't compile, I'm assu$$anonymous$$g those aren't parts of your actual code).
Okay. First Script
Public class $$anonymous$$ovement : $$anonymous$$onobehaviour {
public Transform Joystick;
void Start() {
Joystick = GameObject.Find("Joystick").transform;
}
}
Second Script
public class GetPressed : $$anonymous$$onobehaviour {
private $$anonymous$$ovement movementscript;
void Start() {
movementscript = GetComponent<$$anonymous$$ovement>();
void Update() {
if(movementscript.Joystick.textureRect.Contains(Input.$$anonymous$$ousePosition)){
if(Input.Get$$anonymous$$ouseButtonDown(0))
Send$$anonymous$$essage("$$anonymous$$ove");
}
}
}
Now the interesting thing is that your second script doesn't reference the Joystick variable at all.