- Home /
Parsing String from script A into Enum in script B
I'm making a game object from scratch and adding a script that uses enums on it. I'd like to load a string from a file and convert that string into the enum and set the enum for the new object.
Edit:
Let me try to reexplain from scratch.
I'm trying to implement "user provided content" (custom textures) in an app that's "in production".
These textures are used on "tiles". Each tile has a "Tile" script.
Tile.cs has TileUse and TileType enums. TileUse lets me know what the tile is being used for (as a floor, wall, corner etc). So, I load up this texture, place it on a game object, put Tile.cs on it and I need to change TileUse from floor (default) to what ever the user made it.
//Tile.cs
public enum TileUse {
floor,
wall,
corner,
openPassage,
passage,
endCap,
diagonalWall,
curvedWall,
goldFish
}
//SideParentSorter.cs
ctTransform.AddComponent<Tile>();
//Insert way to change TileUse (enum) on Tile.
//Insert way to change TileType (enum) on Tile.
Being completely self taught programmer I'm 90% sure this is a casting issue and 25% wtf at this point.
@FreeTimeDev, dude, @karlipoppins was asking for you to include the definition of Tile(The enum), not just state what file it is in, which wouldn't really add value to this question.
Answer by Landern · Oct 12, 2014 at 01:17 PM
Enum.Parse works by passing the Type(the enum definition) and the string or optionally the same signature with a Boolean indicating whether or not to observe case sensitivity.
Lets say you have an enum defined as:
public enum Tile
{
Grass1,
Grass2,
Brick3,
Brick8,
Stone,
Granite
}
Lets say you use ToString to store the enum string value into a variable:
string someTile = Tile.Brick3.ToString();
If I want to convert that back to an enum i would use:
Tile tile = (Tile) Enum.Parse(typeof(Tile), someTile);
If i couldn't guarentee that case would match the enum definition, i would use a different overload of the static method of Parse which could ignore the case of the string.
Tile tile = (Tile) Enum.Parse(typeof(Tile), someTile, true);
Remember, the typeof want the Tile enum type, not the property from a class, i think it's fair to assume ctTile.TileUse is either a property or field that contains a Tile enum value,
Hey, thanks for helping out! I've seen similar examples on UA before I posted my own question.
I guess I'm getting confused and lost with this:
I'm throwing the script with the enum on a brand new object at run time. After I instantiate the object, add the script (Tile), I need to change the enums on that new object.
This is what I'm using right now:
GameObject ct = Instantiate (new GameObject(), new Vector3(0f,yPos-offset,1f), Quaternion.identity) as GameObject;
if(ct.GetComponent<GUITexture>() == null)
{
ct.AddComponent("GUITexture");
ct.GetComponent <GUITexture>().texture = Resources.Load<Texture2D>(path+tileSet+type+".png");
ct.AddComponent<Tile>();
ctTile = ct.GetComponent<Tile>();
Tile tileUse = (Tile) System.Enum.Parse(typeof(Tile), usage);
ctTile.TileUse = tileUse;
}
How can I make sure that this particular instance (ctTile) has the correct tileUse? That's why I was using ctTile before.
You need to post your code, enum's are super simple to use and it seems like there is a disconnect some where. I'd very much like to help you. If Tile is an enum, then this entire setup is wrong based off what you posted above and could never work. The reason is in the code, you are using Tile as an enum and a class that has properties like TileUse, that doesn't work. You can have classes that have fields/properties that i have can enum or many enums that you use.
Example:
// File Tile.cs
public enum Tile
{
Grass1,
Grass2,
Brick3,
Brick8,
Stone,
Granite
}
// File SomeClass.cs // Some using statements and junk
public class SomeClass : $$anonymous$$onoBehaviour
{
// This is a field and refers to the enum just above this
public Tile CurrentEnumTileValue;
public void Start()
{
CurrentType = Tile.Granite;
}
public void Update()
{
Debug.Log("Current Tile is: " + CurrentEnumTileValue.ToString());
}
}
I've updated my original post, hopefully with a better description.
@FreeTimeDev, here is my understand now. Not sure why you didn't post Tile.cs, that would have made things easier.
Tile is a class, it derives/inherits from $$anonymous$$onoBehaviour allowing you to use it as a component on a unity GameObject.
It has a field or property that looks one of these two:
public TileUse TileUse; // this is a field and is named the same as the enum type, which is fine but can be confusing.
or
public TileUse TileUse { get; set; } // this is a property.
Now, regardless of which you want to change, you're having an issue doing so. One thing to keep in $$anonymous$$d is that when you use AddComponent, it actually returns the Component class(Your class inherits from $$anonymous$$onoBehaviour, $$anonymous$$onoBehaviour inherits from Component and so on), you can see that , you don't need to get the component again.
If i was going to modify your code, it would look like this:
GameObject ct = Instantiate (new GameObject(), new Vector3(0f,yPos-offset,1f), Quaternion.identity) as GameObject;
// We don't have to check explicity for null, unity overloads the == operator and .Equals method, there is an assumption in the condition of the if statement to check for null
// We will put some error checking here too
if(ct.GetComponent<GUITexture>())
{
GUITexture newGUITexture = ct.AddComponent<GUITexture>(); // Add a new GUITexture Component to ct, AddComponent returns type Component, we will up cast to the type we need in that stack. (up cast/down cast explaination: http://stackoverflow.com/questions/1524197/downcast-and-upcast)
// If newGUITexture is NOT null, lets check its texture
if (newGUITexture)
{
Debug.Log("Successfully created GameObject and added component GUITexture");
Debug.Log("GUITexture added, adding Texture2D(Tile) to GUITexture");
newGUITexture.texture = Resources.Load<Texture2D>(path+tileSet+type+".png");
}
else
{
Debug.LogError("Failed to AddComponent GUITexture to GameObject", newGUITexture);
}
Tile newTile = ct.AddComponent<Tile>(); // Add new Tile type to gameobject
if (newTile)
{
Debug.Log("Successfully created GameObject and added component Tile");
Debug.Log("Tile added, changing TileUse enum to: " + usage);
newTile.TileUse = (TileUse)System.Enum.Parse(typeof(TileUse), usage, true);
}
else
{
Debug.LogError("Failed to AddComponent GUITexture to GameObject", newTile);
}
}