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 Hatdatsat · Sep 19, 2018 at 07:15 AM · variablename

name int with a string

Hello all,

I have looked around but i cant seem to find the answer so i figured ill just go and ask and hopefully someone can help me. So im getting some objects with tags and put them in an array pretty normal, but each object will have a set of variables i want to get with them and i want to name those variables after the object it is related to.

normally i call a float like this:

 float name = 1,0f;

but i want to do it a bit differently:

 string s =  GameObject.name + "Triangle";
 float s = 10,0f;

so this should result in the float being CubeTriangle = 10,0f

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

2 Replies

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

Answer by ShadyProductions · Sep 19, 2018 at 07:36 AM

I think Zodiarc's answer is heading in the right direction and will work for this case. But if you want multiple variables to be part of one object, then you should simply create an object.

 public class GameObjectData
 {
      public GameObject gameObject;
      public float FloatData;
      public string Name;
 }

then you can do simply create your new obj based on your new class:

 var newObj = new GameObjectData() 
 { 
      gameObject = GameObject,
      FloatData = 10.0f,
      Name = GameObject.name + "Triangle"
 };

and u can access and change fields/properties on this object like so:

 newObj.Name
 newObj.FloatData

This is basic C#

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 Hatdatsat · Sep 19, 2018 at 07:41 AM 0
Share

I feel stupid now completely forgot about this facepalms

avatar image Hatdatsat · Sep 19, 2018 at 11:36 AM 0
Share

So for anyone still stopping by to check this out the way i solved what I wanted is like this:

 using System.Collections;
 using System.Collections.Generic;
 using UnityEngine;
 
 public class GameObjectData {
     public GameObject gameObject;
     public string Name;
     public Vector3[] vertices;
     public int[] triangles;
     public Vector3[] verticesWorldPos;
     public Vector3[] trianglesV3;
 }
 
 public class Object$$anonymous$$anager : $$anonymous$$onoBehaviour {
     GameObject[] PaintableObjects;
     GameObjectData[] gameObjectDataArray;
 
     // Use this for initialization
     void Start() {
        PaintableObjects = GameObject.FindGameObjectsWithTag("paintable");
         gameObjectDataArray = new GameObjectData[PaintableObjects.Length];        
 
         for (int i = 0; i < PaintableObjects.Length; i++) {
             gameObjectDataArray[i] = new GameObjectData();
             gameObjectDataArray[i].Name = PaintableObjects[i].transform.name;
             $$anonymous$$eshCollider object$$anonymous$$esh = PaintableObjects[i].GetComponent<$$anonymous$$eshCollider>();
             $$anonymous$$esh mesh = object$$anonymous$$esh.shared$$anonymous$$esh;
             gameObjectDataArray[i].vertices = mesh.vertices;            
             gameObjectDataArray[i].triangles = mesh.triangles;
         }
     }
 }
 
avatar image
0

Answer by Zodiarc · Sep 19, 2018 at 07:22 AM

If I understand you correctly you want make variable names dynamically? Why would you want to do that? That's plain horrible programming and I'm not aware that it's possible in C#.

If you really insist on it, you could youse a Dictionary:

 Dictionary<string, float> namedFloats = new Dictionary<string, float>();
 namedFloats.Add(GameObject.name + "Triangle", 10.0f);


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 Hatdatsat · Sep 19, 2018 at 07:27 AM 0
Share

because I want to make it modular, i don't know how many objects the costumer wants and he wants to be able to add them himself so i want the variables we need with each object to be generated if a new 1 is added. It is a bit of a weird way of doing things but there is a good reason for it, my main question is it possible without going the dictionary route?

avatar image ShadyProductions Hatdatsat · Sep 19, 2018 at 07:41 AM 0
Share

You will have to use some way of caching your variables. Dictionary is the best way to make sure you only have unique variables.

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

95 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

Related Questions

Storing a variable and it's name 3 Answers

Problem naming instances with incrementally numbered counter 1 Answer

Is it bad to use long variable names? 1 Answer

Call static variable dynamically 1 Answer

Converting String to Variable Name 3 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