Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 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 LegatoMan · Mar 07, 2019 at 08:49 PM · scripting beginnervariable-definition

How to dynamically create variable names?

Hey folks, Unity beginner here. Having a blast so far with learning the ropes, and following the tutorials. However, I've run into a problem that I'm not sure how to effectively Google, so here goes:

Background Info: I'm trying to program a game like "Tetris Attack" where differently colored blocks are instantiated in a grid, and those blocks can later be moved around and switched, and if you manage to get three in a row, they disappear.

So as you can see, I'm having to individually spell out each block, creating new variables for each Vector, for each color, and for each GameObject. This works, but considering there will later be around 100+ blocks in the game being constantly destroyed and created, I figure this is a pretty wasteful way to go about it.

My intuition would be to create a for loop, with "counter" variables that let me dynamically create the variable names I need as I go along (A1, A2, A3, etc). But annoyingly, I can't name a variable something that consists of another variable :/

Here is an excerpt of this exhausting naming and instantiating process. I'm assuming there is a better practice, and I'd be grateful for any pointers!

         Vector2 A1 = new Vector2 (0, 0);
         int A1color = Random.Range (0, 5);
         GameObject A1new = Instantiate (colors [A1color], A1, Quaternion.identity);            
         Gridspots.Add ("A1", A1new);
         Colorspots.Add ("A1", new colorclass (A1color));
 
         Vector2 A2 = new Vector2 (0, 1);
         int A2color = Random.Range (0, 5);
         GameObject A2new = Instantiate (colors[A2color], A2, Quaternion.identity);
         Gridspots.Add ("A2", A2new);
         Colorspots.Add ("A2", new colorclass (A2color));
 
         Vector2 A3 = new Vector2 (0, 2);
         int A3color = Random.Range (0, 5);
         GameObject A3new = Instantiate (colors[A3color], A3, Quaternion.identity);
         Gridspots.Add ("A3", A3new);
         Colorspots.Add ("A3", new colorclass (A3color));
 
         Vector2 B1 = new Vector2 (1, 0);
         int B1color = Random.Range (0, 5);
         GameObject B1new = Instantiate (colors[B1color], B1, Quaternion.identity);
         Gridspots.Add ("B1", B1new);
         Colorspots.Add ("B1", new colorclass (B1color));
 
         Vector2 B2 = new Vector2 (1, 1);
         int B2color = Random.Range (0, 5);
         GameObject B2new = Instantiate (colors[B2color], B2, Quaternion.identity);
         Gridspots.Add ("B2", B2new);
         Colorspots.Add ("B2", new colorclass (B2color));
 

et cetera!

Comment
Add comment · Show 2
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 highpockets · Mar 07, 2019 at 09:09 PM 0
Share

Hmmm, have you heard of prefabs. I would think that you could just make a prefab for each shape and that prefab could have a script that uses random range to get its color. Instantiate them when you need them and if you need to save them use a list of GameObjects, but why do you need to save them exactly??

avatar image RobAnthem · Mar 08, 2019 at 03:25 PM 0
Share

Dynamic variables are actually pretty simple. You just use a dictionary like this. Alternatively there's a hundred other ways to do it.

 public Dictionary<string, object> dynamics;
 
 public void AddVariable(string name, object obj)
 {
     dynamics.Add(name, obj);
 }
 public object GetVariable(string name)
 {
     return dynamics[name];
 }


As a side note, when I first started using C# I was desperately looking for a way to create dynamic variable names, and eventually I realized it was actually 100% unnecessary.

1 Reply

· Add your reply
  • Sort: 
avatar image
1

Answer by Tsaras · Mar 07, 2019 at 09:00 PM

You can't create variable names at runtime in a compiled language like c#/c++. What you are looking for is a way to dynamically allocate an arbitrary number of objects which you can point to using one variable name and an index.

This is what Arrays and Lists are used for. In your case, you will benefit from creating a Class that will have the information you need for every shape you spawn and then create instances of this class, which you then can store in an array or list.

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 LegatoMan · Mar 07, 2019 at 09:04 PM 0
Share

Thank you!! This makes a lot of sense.

avatar image mchts · Mar 08, 2019 at 02:53 PM 1
Share

And you could also use Dictionary if you want to have special unique "keys" for your instances. You may check this link for more info.

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

108 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 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 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 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 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 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

2D flash health and damage system 0 Answers

Please Help Me With My Stamina Bar! 1 Answer

Value chages when button is held down, but after release it reverts to old value. 0 Answers

Ghosting effect for sprites 2 Answers

Counting prefabs in screen?,Problem with counting prefabs in screen 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