0

I am writing the bodyType of the assetsToSave gameObjects into the txt file which works perfectly

   assetBodyType[j] = assetsToSave[j].GetComponent<Rigidbody2D> 
 ().bodyType.ToString();

In my txt file there then is the string called "Kinematic" which i am parsing to

   assetBodyType[i] = data[0];

I dont need help on saving or loading because this works already. My only problem is how i would assign the string "Kinematic" to the gameObject since it is a string ?

So my only problem is that i cant call the usual function like this

RigidbodyType2D.Kinematic

since i want to replace Kinematic with

  assetBodyType[j]

to have the actual bodyType and not everything to kinematic how do i put this string into the function of Rigidbody2D.?? How else could i assign it ?

 possibleObjecs[j].GetComponent<Rigidbody2D>().bodyType = 
 RigidbodyType2D.Kinematic; 

I tried searching other methods but couldnt find anything

Safrosoft
  • 14
  • 4

1 Answers1

0

You can use Enum.Parse or Enum.TryParse to parse a string to enum

possibleObjecs[j].GetComponent<Rigidbody2D>().bodyType =
Enum.Parse(typeof(RigidbodyType2D), "Kinematic");

But i suggest a good way to do this is assign all the field that you want to save to a class and use a Json converter to save and load, this is a topic about convert json to enum in object.