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:
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");
}
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:
… 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:
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.
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.