Problem with getting target transform data

Hi,

I’m a newbie wth Unity and is having an issue with getting transform data from a target Object.

My script involves spawning a number of enemies by instantiating them and I had no problem when i had an enemy object in the hierarchy that I used to instantiate. But since I didn’t want to have an enemy object already in my level from the beginning I made a prefab of it and removed it from the hiearchy. But since I used the following line of code to set a target for my enemies to attack:

var target : Transform;

And then assigned the target object (the player by the way) by dragging it into the Inspector panel of my enemy object. But that I cannot do after having made a prefab of my enemy object. Therefore I must assign a target object in my EnemyControl script and change the line above to this:

private var target = GameObject.FindGameObjectWithTag("Player");

… after having tagged my Player prefab. But now what my script gets is not a Transform om the target object but the GameObject itself so I must change the following line:

targetRotation = Quaternion.LookRotation (target.position - transform.position);

to what I would guess:

targetRotation = Quaternion.LookRotation (target.transform.position - transform.position);

But that doesn’t work. I get the followig error message (and Unity or the Web Player crashes if I try it anyway):

Any suggestions on how to resolve this issue?

Regards,

Johan Israelsson

Welcome!

I’m not sure if all your details are the best way to go about this (I’m mostly a newbie myself), so there may be other problems I’m not seeing, but I know that error refers to your “private var” line. You can’t create the variable AND do the Find all at once.

Instead, you could declare it without a value:

private var target : GameObject;

And then define it separately, maybe in an “on Start()” or “on Awake()”.

on Awake ()
{
     target = GameObject.FindGameObjectWithTag("Player");
}

Hope that gets you a little further!

Thanks for the reply. But as far as I can see I’m having no problems with getting access to my target object. My problem is that I suddenly can’t use the following line:

targetRotation = Quaternion.LookRotation (target.position - transform.position);

That gives me the following error:

… wich is logical since I am now getting a whole GameObject and not only a Transform. BUT what I can’t understand is why it still doesn’t work when I change the line to this:

targetRotation = Quaternion.LookRotation (target.transform.position - transform.position);

Then I get the following error that I mentioned earlier:

Any ideas?

Regards,

Johan Israelsson

That’s the caveat: some functions can’t be called at initialization time (because the other game objects might not be initialized yet - e.g. the one you try to refer to).

Instead, write it like this:

var targetRotation : Quaternion;
targetRotation = Quaternion.LookRotation (target.transform.position - transform.position);

I.e. on a separate line - this will automagically put it into Start() function. Or just manually put the code in Start(), Awake() or some other function.

Aah, I see! I got rid of the error now. I will make the other necessary change in order to really try it out.

I’m really sorry for my ignorance! Thank you both!!!

Regards,

/Johan I

Thanks Aras! This helped me realize, In my situation, it was a legitimate error that I was able to fix - c# project btw

I had both a ScriptableObject and MonoBehaviour with [ExecuteInEditMode]. The ScriptableObject had a property I was declaring AND initializing at the same time that relied on the MonoBehaviours singleton instance to be set:

public SystemLanguage language = LocalizationManager.Instance.currentLanguage;

It was calling for a singleton instance on the MonoBehaviour object at design time before Awake or Start had been called. The Instance getter/setter of the singleton had to look for a GameObject in the scene if the instance hadn’t been set yet and bam, there was the problem.

All I had to do was remove the initialization of the property to a point that was AFTER the Awake() commands had run through the scene.