- Home /
get enum value using an object name...
I have a bunch of gameObjects and a "player" moving around that will tag them.
The camera will switch position/rotation based on a List and would like to use an enum to grab the List item holding the position and rotation data. Here is my code snippet...
enum objects { obj0, anotherObj, yetAnotherOne, okLastObj };
List<cameras> cameras = new List<cameras>();
void OnTriggerEnter(Collider thisObject) {
Debug.Log("Hit " + this.name + "! Obj: " + thisObject.gameObject.name);
Camera.main.transform.position = cameras[objects.anotherObj].position;
Camera.main.transform.rotation = Quaternion.Euler(cameras[objects.anotherObj].rotation);
}
This works just fine but does anyone know how I can use the object name (this.name)?
tried using the only thing that keeps coming up in search
objects parsed_enum = (objects)System.Enum.Parse(typeof(objects), this.name, true);
just returns the string value so "anotherObj" and trying to use it in the List call
Debug.Log(cameras[parsed_enum]);
won't compile due to this error (which makes sense since it's a string and not int)
Error CS1503: Argument #1' cannot convert
objects' expression to type `int' (CS1503) (Assembly-CSharp)
How do you use a string to get the enum value so something like objects.this.name instead of hard coding it as objects.anotherObj so I can use it in the List call?
I also found a general C# Enum.Parse but Unity doesn't seem to understand the Parse unless there is another using/include that has that in it.
Your answer
Follow this Question
Related Questions
Using an enum in a class constructor. 1 Answer
Unknown identifier: 'types'. js(38,61) 1 Answer
How to use Enum? 1 Answer
How do I use an enum value as a variable? 3 Answers
Enum-Variable with float 1 Answer