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 TheCoolDog · Jan 18, 2013 at 11:14 PM · variablerandomspawnname

Getting a variable by name

ok, I have six variables that are game objects, spawn1, spawn2 ect. In the script I want to be able to get the variable by name using a random number. So like :

spawn_+random_number2

Please help, here is my script:


 var coin_prefab:GameObject;
 var spawn_1:GameObject;
 var spawn_2:GameObject;
 var spawn_3:GameObject;
 var spawn_4:GameObject;
 var spawn_5:GameObject;
 var spawn_6:GameObject;
 
 private var random_number:int;
 private var random_number2:int;
 
 function Awake()
 {
     InvokeRepeating("spawn", Random.Range(5,15), Random.Range(5,15));
 }
 
 function spawn()
 {
     random_number = Random.Range(1, 2);
     if (random_number == 1) //coin
     {
         random_number2 = Random.Range(1,6);
         pos = "spawn_"+random_number2.ToString().transform.position;
         rot = "spawn_"+random_number2.ToString().transform.rotation;
         Instantiate(coin_prefab, pos, rot);
     }
 }

Comment
Add comment
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

3 Replies

· Add your reply
  • Sort: 
avatar image
2
Best Answer

Answer by justin35f · Jan 19, 2013 at 02:04 AM

I would suggest creating an array to hold your GameObjects. That way you can just pass in random_number2 as the index.

 var coin_prefab:GameObject;
 var spawns : GameObject[];
 
 private var random_number:int;
 private var random_number2:int;
 
 function Awake()
 {
     InvokeRepeating("spawn", Random.Range(5,15), Random.Range(5,15));
 }
 
 function spawn()
 {
     random_number = Random.Range(1, 2);
     if (random_number == 1) //coin
     {
        random_number2 = Random.Range(1,6);
        pos = spawns[random_number2].transform.position;
        rot = spawns[random_number2].transform.rotation;
        Instantiate(coin_prefab, pos, rot);
     }
 }
Comment
Add comment · Show 2 · 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 TheCoolDog · Jan 19, 2013 at 01:30 PM 0
Share

thanks, I didn't know you could have GameObjects as an array.

avatar image justin35f · Jan 19, 2013 at 07:55 PM 1
Share

An array is just a container to hold information, regardless of DataType. I would definitely recommend what @numberkrunch mentioned, and that is to change your GameObject's to Transforms.

avatar image
2

Answer by iwaldrop · Jan 19, 2013 at 12:41 AM

Well, you can't treat variable names like strings. I think you want to try this:

 var coin_prefab:GameObject;
 var spawns : GameObject[];
 
 private var random_number:int;
 private var random_number2:int;
 
 function Awake()
 {
     InvokeRepeating("spawn", Random.Range(5,15), Random.Range(5,15));
 }
 
 function spawn()
 {
     random_number = Random.Range(1, 2);
     if (random_number == 1) //coin
     {
        random_number2 = Random.Range(1,6);
        pos = spawns[random_number2].transform.position;
        rot = spawns[random_number2].transform.rotation;
        Instantiate(coin_prefab, pos, rot);
     }
 }

Then just assign each GameObject to a position in the array (if not in the inspector) like so (replacing all caps stuff w/your actual values):

 spawns[SOME VALUE BETWEEN 0 and 5] = SOMEGAMEOBJECT;
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 justin35f · Jan 19, 2013 at 12:46 AM 0
Share

I posted this same thing, but since I'm new, my answer has been in a moderation queue for well over an hour.

avatar image TheCoolDog · Jan 19, 2013 at 01:32 PM 0
Share

thanks, this answers my question as well but you and Justin Hammond basically had the same answer so I just chose any.

avatar image TheCoolDog · Jan 19, 2013 at 02:02 PM 0
Share

ins$$anonymous$$d of 'spawns[SO$$anonymous$$E VALUE BETWEEN 0 and 5] = SO$$anonymous$$EGA$$anonymous$$EOBJECT;' i'll just assign them in the unity editor.

avatar image
1

Answer by numberkruncher · Jan 19, 2013 at 12:46 AM

I am not sure if this will work in UnityScript, but this does work in JavaScript:

 random_number = Random.Range(1, 2);
 if (random_number == 1) //coin
 {
     random_number2 = Random.Range(1,6);

     var randomSpawner : GameObject;
     randomSpawner = this['spawn_' + random_number2.ToString()];

     // Avoid looking up the same transform component multiple times
     var spawnerTransform = randomSpawner.transform;
     pos = spawnerTransform.position;
     rot = spawnerTransform.rotation;
     Instantiate(coin_prefab, pos, rot);
 }

I would be inclined to reference your spawners using Transform instead of GameObject to avoid the need to lookup the transform components each time.

Though I would use the approach suggested by @iwaldrop (but with Transform[] instead of GameObject[]):

 var coin_prefab:GameObject;
 var spawns : Transform[];
 
 private var random_number:int;
 private var random_number2:int;
 
 function Awake()
 {
     InvokeRepeating("spawn", Random.Range(5,15), Random.Range(5,15));
 }
 
 function spawn()
 {
     random_number = Random.Range(1, 2);
     if (random_number == 1) //coin
     {
        random_number2 = Random.Range(1,6);
        pos = spawns[random_number2].position;
        rot = spawns[random_number2].rotation;
        Instantiate(coin_prefab, pos, rot);
     }
 }
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 justin35f · Jan 19, 2013 at 01:44 AM 0
Share

I agree with your comment about using @iwaldrop suggestion, specifically about using Transform[] rather than GameObject[]. I wrote up a very similar answer, but it's awaiting moderator approval to be posted. I also made the array of type GameObject, rather than Transform. If @TheCoolDog is only using it for the position/rotation information, then Transform is likely the better approach.

avatar image numberkruncher · Jan 19, 2013 at 02:15 AM 0
Share

@justin35f I would imagine that the Transform approach was better in both circumstances because you can access the game object efficiently via transform.gameObject because the reference is stored locally and avoids the expensive GetComponent call that occurs internally when using the .transform property.

avatar image TheCoolDog · Jan 19, 2013 at 02:00 PM 0
Share

Ok thanks I'll use transform ins$$anonymous$$d.

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

12 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

Related Questions

Prefab script values do not update 2 Answers

How to make levels spawn randomly? 0 Answers

why do I need to type variables classes? 2 Answers

Spawning at a random position away from the player 1 Answer

Select from an array based on values 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