- Home /
Prefabs and variables
Hi. I have a prefab of a textured cube (supposed to be a metal plate) and it has some scripts attached to it. One disables the camera orbit script (which is in a different gameobject) when the mouse is over it, and one allows it to be dragged. The reason the camera orbit is disabled when the mouse is over it is because when the player drags it, the camera moves too. It works with the original cube that is already in the scene, but the prefab won't accept the camera as a variable, giving me this error:
UnassignedReferenceException: The variable mouseOrbitScript of 'disableMouseOrbit' has not been assigned. You probably need to assign the mouseOrbitScript variable of the disableMouseOrbit script in the inspector. disableMouseOrbit.OnMouseUp () (at Assets/disableMouseOrbit.js:9) UnityEngine.SendMouseEvents:DoSendMouseEvents(Int32, Int32)
Here is my code:
var mouseOrbitScript : GameObject = GameObject.FindWithTag("MainCamera");
function OnMouseOver () {
if(Input.GetKeyDown(KeyCode.Mouse0)){
mouseOrbitScript.GetComponent(MouseOrbit).enabled = false;
}
}
function OnMouseUp(){
mouseOrbitScript.GetComponent(MouseOrbit).enabled = true;
}
Try to move initialization of mouseOrbitScript
variable to the Start
function.
is it possible you havent assigned an object in the inspector panel for the script you have attached to your gameobject . I could be wrong! If u read what your debugger is saying , thats what it getting at
Answer by Bunny83 · Nov 24, 2013 at 01:36 AM
Like ArkaneX said you should initialize your variables in Start. Also i would store the MouseOrbit script as direct reference and not the gameobject that contains the script:
var mouseOrbitScript : MouseOrbit;
function Start()
{
mouseOrbitScript = Camera.main.GetComponent(MouseOrbit);
}
function OnMouseOver ()
{
if(Input.GetKeyDown(KeyCode.Mouse0))
{
mouseOrbitScript.enabled = false;
}
}
function OnMouseUp()
{
mouseOrbitScript.enabled = true;
}
just to clarify: Camera.main
searches for the first gameobject that is tagged $$anonymous$$ainCamera. So it essentially does the same as
GameObject.FindWithTag("$$anonymous$$ainCamera")
but is shorter and not subject of mistyped tagnames ;)
Thanks to everyone who helped me! Bunny's script worked like a charm!