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 Inan-Evin · Feb 10, 2015 at 03:04 PM · transformobjectsingletonreturn

Return Transform by String Parameter

Hello everyone, I have a singleton class called WorldManager. I want this class to hold some transforms. In the awake function, I create some gameobjects, and also reach some transforms by tags and store them, as a result, after the awake call has finished, I have some Transforms stored in the class. What I would like to do is, I want to reach these transforms whenever I want from another script, this should be easy since the class uses singleton. I know I can do something like this:

 void Awake()
 {
 // storing all transforms like camera,player,light
 
 }
 public Transform GetObject(string objectName)
 {
 if(objectName == "Player")
 return Player;
 if(objectName == "Camera")
 return CameraTransform;
 if(objectName == "Light")
 return Light;
 else
 return null;
 }

I know this works, but I do not want to write an if statement for every object that I want it to be reached, so is there any way like this :

 public Transform GetObject(string objectName)
 {
 return the transform which named objectName ? 
 }


Thanks :)

Comment
Add comment · Show 1
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 Owen-Reynolds · Feb 10, 2015 at 04:06 PM 1
Share

I think you'll end up junking this for a better system, anyway. manager.cam is just as good as manager.getObject("Camera");. And string lookups are a pain (they don't auto-complete.)

2 Replies

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

Answer by Baste · Feb 10, 2015 at 03:15 PM

Yes, you would do this by using a HashMap. The built-in one in C# is Dictionary, which you'll get hold of by putting "using System.Collections.Generic" in the top of your script.

It works like an array, but you index stuff by a custom property - in your case a string - instead of an int. So you'd do:

 Dictionary<string, Transform> itemDict;

 void Awake() {
     itemDict = new Dictionary<string, Transform>();

     itemDict["Player"] = GameObject.FindWithTag("Player").transform;
     ...
 }

 public Transform GetObject(string objectName)
 {
     //Will cause an error if you haven't stored an item with the name objectName
     return itemDict[objectName]; 

     //If you want to be safe, do this instead:
     if(itemDict.ContainsKey(objectName))
         return itemDict[objectName];
     else {
         //handle the objectName key not existing. Like, print an error, return null. 
     }    
 }

Nice and easy. If you're interested, I could tell you why what you're doing is wrong, and is basically something you should never, ever do. Seriously, it's a horrible idea. But if you just want a solution, that's it.

Comment
Add comment · Show 4 · 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 Landern · Feb 10, 2015 at 03:17 PM 1
Share

@Baste, 40 seconds behind you :)

avatar image Inan-Evin · Feb 10, 2015 at 05:08 PM 0
Share

So I got that dictionary is the way, but yes you can tell me why does it cause performance problems, im gonna be using this just in the start call in some classes, im gonna get transforms and store them in another private transforms in other classes, still a bad way ?

avatar image Baste · Feb 10, 2015 at 11:54 PM 1
Share

Essentially, finding the objects you're looking for by the way of a string is a really bad idea. There's some performance issues - comparing strings is a bit expensive - but that's not the big problem here.

The big one is essentially that if any of the strings changes, you'll have no idea where it's been referenced. If you misspell one of the strings, you won't know until you run the code, and then only when you try to run the offending thing.

A much better way, if you have to cache the data somewhere, is just to save the objects directly. Ins$$anonymous$$d of having a dictionary with "Camera" bound to a camera's transform, why not just have the Camera set as a public variable? Or accessible from a public method? You can just do:

 public Transform playerTransform;

 void Awake() {
     playerTransform = GameObject.FindWithTag("Player").transform;
     ...
 }

which is the same thing, but you'll get a compilation error when you spell something wrong ins$$anonymous$$d of the game blowing up at you at some completely unrelated point because the string sent in didn't match.

avatar image Inan-Evin · Feb 11, 2015 at 11:34 AM 0
Share

Alright I got it totaly right now, thanks for the answers :).

avatar image
1

Answer by Landern · Feb 10, 2015 at 03:17 PM

Use an enum and dictionary. A dictionary can't have duplicate keys, so keep that in mind.

an example

 Dictionary<ObjectTransformName, Transform> objectDic;
 
 public Awake()
 {
     objectDic = new Dictionary<ObjectTransformName, Transform>();
 
     objectDic.Add(ObjectTransformName.Player, GameObject.FindGameObjectByTag("Player").transform);
     objectDic.Add(ObjectTransformName.Camera, Camera.mainCamera.transform);
     // etc etc
 }
 
 public enum ObjectTransformName
 {
     Player,
     Camera,
     Light,
     // others
 }
 
 public Transform GetObjectTransform(ObjectTransformName otn)
 {
     // Do some checking here for null and objectDic.Count > 0 or even the key existing
     return objectDic[otn]
 }


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

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

Huge objects dissapear 1 Answer

moving random objects to random positions 1 Answer

How to make a object face another object 1 Answer

Move Object to location of Trigger? 1 Answer

How do I replace an old gameObject with a new one while keeping the functionality of the old one?,How do I replace my current gameObject with a new one while maintaining the functionality of the old one? 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