- Home /
Passing Values/Variables between Classes
Ive been checking the help of unity and cant find what im looking for so im here XD sry if i just didnt knew how to search and it was there(if thats the case) im working in javascript btw ok the question is i have 3 objects top object bottom object mid object(i just made this to get the variables of the top and bot) ok the thing is when your mouse enters the top object this happens
function UpTrue(Enter:boolean)
{
if(Enter)
{
Count +=1;
}
else
{
Count =0;
}
}
count is declared like this at the top
var Count: int =0;
its the same for bottom so i was making this at the mid one thats the class taking all the info or at least suppossed to
var targetHand: GameObject; var Up : MouseEnterUp;//name of script var Bottom : MouseEnterBottom;//name of script function Start(){ targetHand= GameObject.Find("Hand"); }
function Update() { if(Up.Count >= 1) { targetHand.animation.wrapMode = WrapMode.PingPong; targetHand.animation.Play("clean"); // name of the animation clip } }
so what im trying to do basically is getting Count from object at top and bottom and if they are both 1 or at least for now if top is = 1 then play the animation in case it helps im getting the error NullReferenceException: Object reference not set to an instance of an object MidObject.Update() MidObject is the name of the script attached to the middle object
please any help would be really appreciated if you dont understand anything tell me and i will try to make it better so you can understand :S thank you very much\
since it seems like its kinda hard or something i dont know if theres a way to know the value of a function from other script >.< if theres anything tell me please thank you
To make it a little clear i want to make this if i made it in c++ it would be public bool UpTrue(Enter) { return Enter; }
Answer by DaveA · Jan 26, 2011 at 10:52 PM
If I followed your question correctly, it sounds like Up is not assigned to a particular object. Unless the Up script is in the Plugins folder, you'll want to either assign it by GameObject.Find("you object with up script on it"); or drag/drop in the Inspector the object onto that Up variable.
well Up And Bottom are both in a different object so parenting them is not really the answer ive been looking for :S because with Find i dont know if you can use it to get the value of a function inside of the script :S i will try it anyways ty
I don't mean parenting in the Hierarchy, but dragging the object onto the variable name in the Inspector, which may not even work. Use Find. Once you find the the object, use GetComponent to get the script, then you can access any public variables it has.
Answer by kennypu · Feb 12, 2011 at 12:36 AM
its because you declare Up and Bottom, but you never initialized it. so you're doing the same thing as var aNumber:int; but never set it to anything. Assuming that Up and Bottom is attached to the current gameobject, just put this in the Start()
function Start()
{
Up = gameObject.GetComponent("MouseEnterUp");
Bottom = gameObject.GetComponent("MouseEnterBottom");
}