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 KazooCar69 · May 28, 2015 at 01:41 AM · variables

Scaling code to take in variables without using reflection... How to do it?

So lets say I have a script called GameManager and one called Ball. I instantiate GameManager onto one object in my scene, and a bunch of GameObjects with Ball, each behaving differently.

I want ball to change a variable in GameManager (like blueBallDead for example to check to see if a specific ball is disabled), but since (the only way I know how to) add more variables to GameManager is actually hardcoding more in. So if I want to add a new object in my scene that uses Ball in a modular way, I keep having to go back to hardcode more functionality and longwinded if statements into my manager.

Better way to approach this? The only semi-helpful thing I've found was just adding variables into the manager and using reflection on ball in a way where I pass in the variable name as the name of the ball or something, that way I don't have to keep changing ball around. But reflection sucks and is pretty slow.

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

1 Reply

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

Answer by crohr · May 28, 2015 at 02:03 AM

 public class GameManager
 {
     private static Dictionary<string, Ball> _balls = new DictionaryBase<string, Ball>();
 
     public static void AddBall(Ball ball)
     {
         if(!_balls.ContainsKey(ball.Id))
         {
             _balls.Add(ball.Id, ball);
         }
     }
 }
 
 public class Ball
 {
     public string Id;
     public bool IsAlive;
 
     void Start()
     {
         GameManager.AddBall(this);
     }
 }

With a Dictionary you can add new Ball types easily.

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 KazooCar69 · May 28, 2015 at 05:58 AM 0
Share

Thanks, surprised you were able to figure out what I was after. Appreciate the detailed and quick response, got what I needed up and running quick enough.

So I basically just write a few more methods to get data by feeding in a string argument that'll match the ID(key) of the entry in the dictionary, yeah? 'Least that's how I got it working. I forgot how useful static was too, making me able to call this stuff without an explicit reference to an instance of the levelmanager since they'd all be using the same static data.

Is the purpose of dictionary basically to be a List/Array but tying together entries with ID keys?

Sorry for the super noob questions, I've been coding for a few years now casually but I've been using pretty basic methods of getting what I need done. :P

avatar image crohr · May 28, 2015 at 06:28 AM 0
Share

You can add a GetBall(string id) and RemoveBall(string id). I guess what I was imagining in my head when I read your question was you would have several different ball prefabs, each one having a unique Id set in the inspector.

A Dictionary is an indexed collection of $$anonymous$$eyValuePairs (Basically a map from the key to the value.) Accessing a value by key is much faster than iterating through an Array or List.

There is no such thing as a "super noob" question :P as long as you are trying to learn.

Good luck with you project!

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

20 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

Related Questions

How can I assign a variable to an object? 1 Answer

Does Garbage Collector Collect variables Made Inside Scope? 1 Answer

How do I access variables of prefabs I am loading via Resource.LoadAll, in script(C#) 2 Answers

Creating a 'rotation' system 1 Answer

[C# Code] Why am I getting this error??? 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