• 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
Question by Spider_newgent · May 20, 2015 at 10:49 AM · javascriptinstantiategetcomponentfindwithtag

Set a variable in another object at point of instantiation.

I'm making a whack-a-mole game and when a timer reaches 0 I call my Spawn() function. This does the following:

  1. selects a random spawn point from a list.

  2. removes that spawn point from the list temporarily.

  3. creates a mole object at the spawn points position

  4. finds a mole with tag "mole" in the scene

  5. sets the spawnpoint variable of that mole to whichever point was removed from the list.

This all works fine whilst I'm using one "mole." However, when spawning more than one in the scene at once, point 4 above is giving me problems. using FindWithTag just finds any mole object in the scene, which sometimes passes rsp to the wrong mole.

Is there a way to set the mySpawnPoint of the mole at the point it is instantiated, rather than use FindWIthTag?

If any additional code is useful, please let me know.

Thank you.

My code for the function is below:

 function Spawn() 
 {
 rsp = SpawnPointList[Random.Range(0,SpawnPointList.Count)]; //rsp = random spawn point picked from the list 
 SpawnPointList.Remove(rsp);                                    // remove the selected spawn point from the list, preventing it from being selected again whilst mole is there
 //Debug.Log("the spawn point removed from the list is: " + rsp);
 
 Instantiate(mole,rsp.transform.position,rsp.transform.rotation); //create mole at rsp
 var mole = GameObject.FindWithTag("mole").transform;            //find and use mole in the scene
 
 var script: MoleScript = mole.GetComponent(MoleScript);            //assign script to the moles moleScript
     script.mySpawnPoint = rsp;                                    //access the "myspawnpoint" variable inside the mole
 }




Comment

People who like this

0 Show 0
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

1 Reply

· Add your reply
  • Sort: 
avatar image
Best Answer

Answer by Spider_newgent · May 20, 2015 at 12:48 PM

Okay, so I figured this one out myself.

For anyone else who encounters the same problem, instead of using FindWIthTag, just declare "mole" at the point of instantiation, rather than instatiating it and setting the value of "mole" afterwards. I've posted the amended spawn function below:

 function Spawn() 
 {
 rsp = SpawnPointList[Random.Range(0,SpawnPointList.Count)]; //rsp = random spawn point picked from the list 
 SpawnPointList.Remove(rsp);                                    // remove the selected spawn point from the list, preventing it from being selected again whilst mole is there
 
 var mole:Transform = Instantiate(mole,rsp.transform.position,rsp.transform.rotation); //THIS IS THE CHANGED LINE.
 
 var script: MoleScript = mole.GetComponent(MoleScript);            //assign script to the moles moleScript
     script.mySpawnPoint = rsp;                                    //access the "myspawnpoint" variable inside the mole
 }

Comment

People who like this

0 Show 0 · 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

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.

Welcome to Unity Answers

If you’re new to Unity Answers, please check our User Guide to help you navigate through our website and refer to our FAQ for more information.

Before posting, make sure to check out our Knowledge Base for commonly asked Unity questions.

Check our Moderator Guidelines if you’re a new moderator and want to work together in an effort to improve Unity Answers and support our users.

Follow this Question

Answers Answers and Comments

19 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 avatar image avatar image avatar image

Related Questions

Variable of prefab has not been assigned 2 Answers

Want to add ship's velocity to Instantiated projectile 2 Answers

Why am I getting a nullReferenceException? 1 Answer

Change a variable of an Instantiated object 2 Answers

How do I instantiate at a certain point? 2 Answers


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