Getting the rotation of an object from another object within a prefab.

I’m not really even sure how to go about asking this, but here goes. My overall goal is to make a character’s eyes move by adjusting the mainTextureOffset by using the rotation of some bones in the character’s eyes as the x and y values.

So I am trying to assign a transform variable on a script that I have attached to a character’s eyes. Problem is that the character is created from a prefab on start, so I can’t simply drop the object in the transform slot. I need to assign the transform through scripting. The eye bone is in the same object as the eye with the texture, but down a different branch of the hierarchy. So how would I access the rotation transform for the eye bone?

Here is my script so far. It works as I need it to with the exception of assigning that transform variable.

var eyeBone : Transform;

function Start ()
{
	eyeBone = //Not sure what I need here.
}

function Update ()
{
	var eyeBoneAngleX = eyeBone.transform.localEulerAngles.x;
	var eyeBoneAngleY = eyeBone.transform.localEulerAngles.y;

	renderer.material.mainTextureOffset = Vector2(eyeBoneAngleX,eyeBoneAngleY);
}

First try making an instance of the object in your scene and then just drag the child object “Eyes” into the public transfrom then at the top of the Inspector there is an apply button click that and this reference will be saved in the prefab.

or

on line five where you wrote

 eyeBone = //Not sure what I need here.

you can use something similar to

 eyeBone = transform.Find("eye").transform

now i dont know what the path is to your eye bone but you may need to path from the object with your script to it like such

 eyeBone = transfrom.Find("Spine1/Spine2/Neck/Head/Eye").transfrom

you could also make a tag called eye and tag the bone with it then you can use

 eyeBone = transform.FindObjectWithTag("eye");

You could just name the child object something like “Eyes” or something then do a
transform.root.FindChild(“Eyes”);

I have been digging through forums and manuals for the past few hours and finally figured it out. Runalotski, you were very close with

eyeBone = transform.Find("Spine1/Spine2/Neck/Head/Eye).transform;

I tried this early on, but it still wasn’t working right, because it needs to start from the root. So I finally learned I needed to add in “root” to let it know that I wanted it to start searching from the top of this object’s hierarchy.

eyeBone = transform.root.Find("Spine1/Spine2/Neck/Head/Eye").transform;

This finally gave me the result I was looking for.