Hey guys, I am doing it wrong. I have model where I track one of its bones’ rotation and translations in the update function. I want those values to be accessible to another script on a button I made that will instantiate a laser beam. I have tried a quite a few things and nothing has been successful. Could someone tell me a better method… or more accurately a working method to complete this task.
This is the code on the rigged model:
function Update ()
{
var eyeNode = transform.Find("Control_Root/Control_Head/Control_EyeScale/Bone_Eye_Scale/Control_Eye/Bone_Eye");
var eyeNodePos = eyeNode.rotation;
var eyeNodeRot = eyeNode.position;
}
Is unity access variables other script - Google Search any help?
It really doesn’t matter what you’re trying to do here, or what “laser beams” you’re trying to fire. The question you want to ask is “How do I access a variable in one script from another”, unless I’ve misread your question.
Are you having any trouble with the two vars.
var eyeNodePos = eyeNode.rotation;
var eyeNodeRot = eyeNode.position;
Cause unless your trying to do so. Rot would go wth rotation, and Pos would go with position. To expose these vars in the other script there is a handful of ways to do so. Since the vars you have are local/inside the function. You could use a couple of static vars, then compare the local vars to the static ones (eg)
static var eyeNodeP : float;
function Update() {
eyeNodeP = eyeNodePos;
}
Also for each var you may want to just use on parameter at a time (eg) Unless you have some constructor built to house all par.
var eyeNodeRot = eyeNode.rotation.y;
HA omg I missed that when typing. Thanks black mantis! I will give this a try.
I have a new problem now. I need my static var to update with the position of the node on every frame. With my current code it sits at 0. When I debug the values of the axis in the Update function it of course works. How do I get those values in a static var?
Here is what it looks like:
static var eyeNodeX : float;
static var eyeNodeY : float;
static var eyeNodeZ : float;
function Update ()
{
var eyeNode = transform.Find("Control_Root/Control_Head/Control_EyeScale/Bone_Eye_Scale/Control_Eye/Bone_Eye");
var eyeNodeX = eyeNode.position.x;
var eyeNodeY = eyeNode.position.y;
var eyeNodeZ = eyeNode.position.z;
//Debug.Log(eyeNodeY);
}
You have “var” in front of those variables, which means you’re creating a local variable with those names, not assigning to the static one.
Also, you really don’t need to be finding that “eyenode” transform every single frame. Just set it once somewhere during init and access it from there. That way, you won’t need to do any of this stuff in that update() function, just access the position of it from your other scripts when you need it (ie, when the user presses a button). You should try to avoid creating variables in the Update() function if possible, or doing something like a Find() in there.
Thanks xomg I will attempt a better method of doing this.