- Home /
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
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#
I feel stupid now completely forgot about this facepalms
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;
}
}
}
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);
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?
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
![](https://koobas.hobune.stream/wayback/20220612174902im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
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