Hello, I was wondering if it’s possible to assign a new variable to the enum of another script. Basically I have my game handler that has an enum for Item Type. I then want each item to belong to one of those types, indicated by selecting from an enum in the inspector of the item. I want this enum variable and the enum variable in the game handler to be the same. Here’s the code version of what I’m trying to do:
public class GameController : MonoBehaviour {
public enum plantTypes {Squash,GroundVeggies,Trees};
Start(){}
Update(){}
...
}
public class Plant : MonoBehaviour {
public enum plantTypes = GameObject.FindGameObjectWithTag("GameController").GetComponent<GameController>().plantTypes ;
Start(){}
Update(){}
...
}
You can declare the enum outside a class declaration. This way it will be accessible in the whole assembly (all C# scripts).
// file "GameController.cs"
public enum PlantType
{
Squash,
GroundVeggie,
Tree,
nPlantTypeCount
}
public class GameController : MonoBehaviour
{
// some code here
}
// file "Plant.cs"
public class Plant : MonoBehaviour
{
PlantType plantType = PlantType.Tree;
}
hathol
2
Instead of trying to copy over the whole enum (not even sure if that’s possible), just access it directly.
public class Plant : MonoBehaviour
{
GameController.plantTypes someVariable = GameController.plantTypes.Trees;
Start() {}
}
public PlantType typeOfPlant; // you were missing this line.
public enum PlantType
{
Squash,
GroundVeggie,
Tree,
}