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 FreeTimeDev · Oct 12, 2014 at 04:16 AM · stringenumparse

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.

Comment
Add comment · Show 3
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 karlipoppins · Oct 12, 2014 at 05:08 AM 0
Share

Where is the definition of your enum?

avatar image FreeTimeDev · Oct 12, 2014 at 12:55 PM 0
Share

In a separate script called Tile.

avatar image Landern · Oct 12, 2014 at 01:19 PM 1
Share

@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.

1 Reply

· Add your reply
  • Sort: 
avatar image
0

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,

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 FreeTimeDev · Oct 12, 2014 at 04:41 PM 0
Share

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.

avatar image Landern · Oct 12, 2014 at 06:07 PM 0
Share

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());
   }
 }





avatar image FreeTimeDev · Oct 12, 2014 at 07:12 PM 0
Share

I've updated my original post, hopefully with a better description.

avatar image Landern · Oct 13, 2014 at 12:35 PM 0
Share

@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);
     }
 }


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

30 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

Related Questions

Convert int to string and back again 2 Answers

Strings, Arrays and Split in js 1 Answer

How do you get the results from an enum in a different script 1 Answer

Parse String and Serialise 1 Answer

Rich text character-by-character shows tags. How to hide them? 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