- Home /
How can I set a GameObject variable on an instantiated object? (It always instantiates empty!)
Hey guys, In short my enemy is instantiating with a script in which a GameObject variable is created empty when I always want it to be the same. How can i make each enemy spawn with the GameObject filled in correctly each time? Thanks in advance!
More Details if necessary-
The script below is on the enemy and makes it seek out my character. But when it instantiates an enemy clone, the variable "agent : NavMeshAgent" is empty. How can i assign it to always be my character GameObject? Any help would be great.
Here is the code on the enemy (working fine originally on character)..
var dude : Transform;
static var agent : NavMeshAgent;
function Start ()
{
agent.destination = dude.transform.position;
}
function Update ()
{
CheckLocation();
}
function CheckLocation()
{
agent.destination = dude.transform.position;
}
$$anonymous$$ake the object you are cloning, a prefab, but first make sure you have the agent var filled in the inspector. I am just thinking maybe it is not storing that var for your clones because it isn't prefabbed.
Thanks for your quick reply but no, its definitely prefabbed and the var filled. Is there a way I can permanantly make the variable filled? Like ins$$anonymous$$d of having var dude : GameObject, it would be var dude : GameObject(dudePrefab)?
Hmmm, try dragging the 'agent' into the var, but the prefab from your project files, not the agent from your hierarchy. And then prefab the object with the script again.
Awesome dude, it was dragging the prefab from the project folder that solved it. Thanks a bunch!
One last question though- The enemies are all moving towards the point where my character spawns ins$$anonymous$$d of where I currently am now. What would the javascript be to update it every frame ins$$anonymous$$d of dude.transform.position? How would I get my current position?
Answer by bpears · Sep 29, 2013 at 02:04 PM
Also, make sure your script that instantiates the clones has an updated prefab of your enemy, dragged in from project files as prefab, not the one in hierarchy.
//Try it like this, not sure why it isn't updating for you
var dude : Transform;
static var agent : Nav$$anonymous$$eshAgent;
function Start ()
{
agent.destination = dude.transform.position;
}
function Update ()
{
agent.destination = dude.transform.position;
}
Hmm no that seems to have the same result. Is there a query i could run in the update function to find the dude's position on every frame? Thanks heaps for your help as well.
it should be doing so in the function Update() which is called every frame. $$anonymous$$ake sure the Player ('dude') you are moving around is the one you have drag and dropped into the script.
Your answer
Follow this Question
Related Questions
How should i add raycast to multiple game objects??? 0 Answers
NavMesh giving jerky like motion 0 Answers
Spawn items around GameObject 3 Answers
Problem with spawn object 0 Answers