Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by sb1984 · Sep 29, 2013 at 12:03 PM · gameobjectinstantiatespawnnavmeshagentvariables

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;
 }
Comment
Add comment · Show 5
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image bpears · Sep 29, 2013 at 01:42 PM 0
Share

$$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.

avatar image sb1984 · Sep 29, 2013 at 01:55 PM 0
Share

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)?

avatar image bpears · Sep 29, 2013 at 02:02 PM 0
Share

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.

avatar image sb1984 · Sep 30, 2013 at 12:38 AM 0
Share

Awesome dude, it was dragging the prefab from the project folder that solved it. Thanks a bunch!

avatar image sb1984 · Sep 30, 2013 at 12:56 AM 0
Share

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?

1 Reply

· Add your reply
  • Sort: 
avatar image
0

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.

Comment
Add comment · Show 3 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image bpears · Sep 30, 2013 at 01:01 AM 0
Share

//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;
 }
avatar image sb1984 · Sep 30, 2013 at 01:06 AM 0
Share

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.

avatar image bpears · Sep 30, 2013 at 01:12 AM 0
Share

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

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

16 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

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

How to spawn gameobject from host to all clients 1 Answer


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges