- Home /
Issues with an enum not passing
So I have a block of code which is being passed an enum from anothe c# script. Everything is working fine apart from one little thing which is settng leftHandState and rightHandState to the passed enum value. I have debug.log the incoming variables hLeft and hRight and they return the correct enum. But when I go to make leftHandState or rightHandState = to the passed enum value they do not change but stay at the default value for the enum.
This is the code for the specific file below:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public enum HandStates { Default, Fist, ThumbUp, PointingThumbUp, Pointing, IndexFinger };
public class SystemManager : MonoBehaviour
{
//GameObject States
public CasualtyState casualtyState;
//hand Presence
private HandStates leftHandState;
private HandStates rightHandState;
public WristReturnMenu wristReturnMenu;
public void SetLeftHandState(HandStates hLeft) {
Debug.Log(hLeft); //This is logging the correct enum value
leftHandState = hLeft;
Debug.Log(leftHandState); //This isn't changing
}
public void SetRightHandState(HandStates hRight) {
rightHandState = hRight;
}
private void Update() {
Debug.Log("Left Hand State: " + leftHandState);
Debug.Log("Right Hand State: " + rightHandState);
}
}
Answer by darksider2000 · May 14, 2020 at 11:53 PM
Since you're accessing the Set functions from outside of the SystemManager class that means you have another enum called HandStates, and that's why you're having this problem.
Those are two different enums. You have a SystemManager/HandStates and a "OtherCode"/HandStates. You can't make them interact in the same way that you can't do Vector3 = Vector2.
The quickest way for you to fix this in your code would be to pass along an int Index instead of the whole enum type, then use the index to assign the value you want to SystemManager/HandStates.
The correct way would be to create a "BodyTypes".cs scriptable object class. Then make your HandStates enum there, and use that type to create enums in all your code.
public class BodyTypes : ScriptableObject
{
public enum HandStates {ThumbsUp, ThumbsDown};
}
This is how you would declare a new HandStates type in the rest of your code:
public BodyTypes.HandStates thisHandState;
public class SystemManager : MonoBehaviour
{
//GameObject States
So. I implemented that and The Debug.logs both work inside the method now, but if I check the leftHandState from anywhere else in the script it only logs as the default value? for example checking it in Update() in the System$$anonymous$$anager only returns it as Default?
I've found the issue, there was a typo in my code which I've now fixed. it is all working now! you help has been much appreciated
Your answer
Follow this Question
Related Questions
Problem with second enum selection inspector 0 Answers
How to use EnumPopup 1 Answer
Enum-Variable with float 1 Answer
Error calling enum lists 1 Answer
Get Enum member from int 1 Answer