Problem pointing to instantiated object in script..

I’m trying to get a camera script to have the camera point to an instantiated object but it won’t work. No error message, the exposed variable just remains empty when running in the editor so I have to manually drag the instanced object onto the variable during run-time to get it to work. The stripped-down code looks like this:

var target : Transform;

function Awake()
{
	target = gameObject.GetComponent("CamFocus");
}

Function LateUpdate() {

	transform.LookAt (target);
}

Anyone have an idea what I’m doing wrong here?

Well, the target is being initialized successfully, as the GetComponent you’re running returns null. If you’re looking for a gameObject internal to your gameObject, then you can’t use GetComponent.

You’re trying to assign the component “CamFocus”, presumably a script, to the “target” variable, which you have declared as a Transform. You want to assign an actual transform to the target variable; a script can’t be a transform.

–Eric

Mm… okay, the CamFocus is not a script but an empty GameObject made a child of a Player prefab. This prefab contains one mesh and the empty GameObject I want the camera to point at. I tried swapping this line:

target = gameObject.GetComponent(“CamFocus”);

with this:

target = transform.GetComponent(“CamFocus”);

and this:

target = gameObject.GetComponent(“Player(Clone)/CamFocus”);

… and it still behaves the same.

A GameObject that’s the child of another object isn’t a Component, it’s still a GameObject. In this case you’d probably use transform.Find.

–Eric

Ok, tried swapping GetComponent() for transform.Find like so:

try 1:
target = transform.Find(“CamFocus”);

try 2:
target = transform.Find(“Player/CamFocus”);

try 3 (instance gets renamed with (Clone) at the end):
target = transform.Find(“Player(Clone)/CamFocus”);

But still nothing. Rrr… seems I’m always getting into trouble trying to reference anything outside of a script’s own gameObject. How can something so fundamental as communication between objects be so hard to achieve in script? Anyhow, thanks for helping out, I appreciate it :slight_smile: Just getting frustrated is all.

I just gave

var target : Transform; 

function Awake() 
{ 
   target = transform.Find("CamFocus"); 
} 

function LateUpdate() { 

   transform.LookAt (target); 
}

a quick try, and “target” is indeed assigned as expected. However, the camera spazzes out quite spectacularly…since the target is a child of the camera, whenever the camera looks at the child, the rotation causes the child to rotate as well, so the camera rotates more to try to look at it again, etc. etc. You probably don’t want it to be a child. :wink: In which case you can use GameObject.Find or just drag it onto the variable yourself. Referencing other objects is actually quite trivial once you get the hang of it.

–Eric

You have three cases, none of which are that hard to get when you know the techniques.

First case: You have an object as a child of another object in the editor and want a link to it. If that’s the case, then the child object can be dragged directly onto the variable for the script. Simple.

Second case: You have an object Instantiated in-game and want to get a link to it. Well, if you want a link in the script that brings it into the world, then just do this:
target = Instantiate(stuff, stuffPosition, stuffRotation);

Third case: Another script instantiates the object and you need to find it remotely with the first script. In this case, the easiest answer would be to tag it with something like “FindMe” and use:
target = GameObject.FindWithTag(“FindMe”).transform;

Alternatively, if it’s a child of the object the script is on, I think there’s a way to go into the object like it was array. Something like:

for (stuff in transform) {
   if (stuff.name == "TheNameImLookingFor") {
      target = stuff;
   }
}

@Eric5h5
The target’s not a child of the camera, if it was then yeah, rotation mayhem would happen real quick :slight_smile:

@GargerathSunman
Yep, the third-case scenario seems to be what applies here. I tried creating a “CameraFocus” tag and then using "target = GameObject.FindWithTag(“CameraFocus”).transform; ", but get a NullReference exception on running it. Hmm… maybe it’s because the Awake() function in the Camera script is called before the CamFocus object is instantiated?

Not a big fan of mixing scripting with drag’n’drop in the editor TBH. To me this kind of workflow just feels so err, unclean. When debugging for instance, you can’t keep track of what’s going on by looking at the scripts alone, you have to flip back and forth between scripts and the editor to see what’s assigned to and controlled by what. This is why I want to do as much as possible directly in scripts but that’s beginning to seem like a big pain too. But enough with the whining… I’ll do some testing with a simpler scene to try and pin the problem down.

Thanks both for your help :slight_smile:

EDIT:
Aha! When I put the line “target = GameObject.FindWithTag(“CameraFocus”).transform;” in the LateUpdate() function it finally found the target. Great! Thanks again.

It’s the opposite of unclean. :wink: Trying to ignore public variables in the inspector means missing out on a great deal of flexibility and convenience–sort of like tying one hand behind Unity’s back–but you are of course free to use whatever style you like.

–Eric

If the CamFocus object is being created in an Awake function in another script, you can not know in which order the Awake functions are being called.

You should put the FindWithTag code into a Start function. All Start functions are called after all Awake functions have been called.