- Home /
Passing chosen enum value to other script's public method as parameter.
Hi, I'm new in trying to pass enum value to pass to prefab's script. How can I achieve this? I did some research but I'm still confuse/how to do this.
Here's my script:
AIRespawnController: (My Respawn Manager)
Focus: clone.GetComponent <AIDectectionController> ().setSensor (/* how to pass enum here? Tried 'forceType' not working */);
public class AIRespawnController : MonoBehaviour {
public GameObject respawnPrefab;
public bool respawnActive = true;
public int respawnLimit;
public bool destroyInLimit;
public float respawnStart;
public float respawnGap;
//public int existingLimit;
public enum forceOf
{
ally,
//Detect enemy and neutral enemy;
allyCompanion,
//First priority: detect enemy and neutral enemy; Second priority: detect player;
allyReliever,
//First priority: detect player; Second priority: detect ally; Recommend for heal and buffs provider!
enemy,
//Detect player, ally, and neutral enemy;
enemyCompanion,
//First priority: detect player, ally, and neutral enemy; Second priority: detect fellow enemy;
enemyReliever,
//Detect enemy; Recommend for heal and buffs provider!
neutralEnemy,
//Detect player, ally, and enemy;
device,
//Detect all: player, ally, enemies and neutralEnemy;
} public forceOf forceType;
void Start () {
if (respawnPrefab != null)
StartCoroutine (Respawn ());
}
IEnumerator Respawn () {
yield return new WaitForSeconds (respawnStart);
int ctr = 1;
while (respawnActive && respawnLimit >= ctr)
{
GameObject clone = Instantiate (respawnPrefab, transform.position, transform.rotation) as GameObject;
if (clone.GetComponent <AIDectectionController> () != null)
{
clone.GetComponent <AIDectectionController> ().setSensor (/* how to pass enum here? Tried 'forceType' not working */); // <----------------------MY PROBLEM
}
ctr++;
yield return new WaitForSeconds (respawnGap);
}
if (destroyInLimit && respawnLimit < ctr){
Destroy (gameObject, 2f);
}
}
}
AIDectectionController: (Planning to manipulate) [No problem here]
Focus: public void setSensor (sensorOf sensor) { //Overwrite sensorType = sensor; }
public class AIDectectionController : MonoBehaviour {
//Directly go to target when targetLock initialize to true as the game starts or newly instantiating the Object;
public bool targetLock = false;
//Recommended for non moving object or moving things that always want to bump the target;
public bool constantTowardsTarget = false;
//If set to null, then targetObject set to player;
public GameObject targetObject;
//Sense GameObject with tag in Inspector: Player, Ally, Enemy, neutralEnemy;
public enum sensorOf
{
ally,
allyCompanion,
allyReliever,
enemy,
enemyCompanion,
enemyReliever,
neutralEnemy,
device
}; public sensorOf sensorType;
private float changeManeuverDistance;
public float maneuverTargetInRangeMIN = 2f;
public float maneuverTargetInRangeMAX = 5f;
private bool standBy;
AIFireController AIFire;
void Start () {
...
}
...Some Code...
void OnTriggerStay2D (Collider2D other)
{
if (targetObject == null && targetLock == true)
{
targetLock = false;
transform.Find ("Radar_OrangeCircle").gameObject.transform.localScale /= 2f;
return;
}
if (gameObject.CompareTag ("Ally"))
{
if (sensorType == sensorOf.ally)
{ //Detect enemy and neutral enemy;
if ((other.gameObject.CompareTag ("Enemy") || other.gameObject.CompareTag ("NeutralEnemy")) && targetLock == false)
TargetAcquire (other.gameObject);
}
else if (sensorType == sensorOf.allyCompanion)
{ //First priority: detect enemy and neutral enemy; Second priority: detect player;
if ((other.gameObject.CompareTag ("Enemy") || other.gameObject.CompareTag ("NeutralEnemy")) &&
(targetObject != other.gameObject.CompareTag ("Enemy") && targetObject != other.gameObject.CompareTag ("NeutralEnemy")))
{
TargetAcquire (other.gameObject);
}
else if ((other.gameObject.CompareTag ("Player") && targetLock == false) ||
((other.gameObject.CompareTag ("Enemy") || other.gameObject.CompareTag ("NeutralEnemy")) && targetLock == false))
{
TargetAcquire (other.gameObject);
}
}
...Some Long Code....
public void setSensor (sensorOf sensor) // <----------------------PUBLIC METHOD HERE
{ //Overwrite
sensorType = sensor;
}
}
Thank you so much!
Answer by Commoble · Mar 19, 2017 at 06:54 PM
You have two different enum types (forceOf and sensorOf), but they look identical to me. You're probably better off just using one enum type for both classes. For example, let's say you remove sensorOf and keep forceOf. You can declare variables and have function parameters of type forceOf by using AIRespawnController.forceOf. Example:
// in the AIDetectionController class
public AIRespawnController.forceOf sensorType;
public void setSensor(AIRespawnController.forceOf sensor)
{
sensorType = sensor;
}
Hi, @Commoble, thank you for the answer... I tried it, that actually works.
As this also work as solution, but with this I declared 2 enum types, 1 each class, and must be the same order of enum values.
public enum sensorOf
{
ally,
allyCompanion,
allyReliever,
enemy,
enemyCompanion,
enemyReliever,
neutralEnemy,
device
};
// in the AIRespawnController class
// convert enum (forceOf) to int
if (clone.GetComponent <AIDectectionController> () != null) {
clone.GetComponent <AIDectectionController> ().setSensor ((int) forceType);
}
// in the AIDectectionController class
public sensorOf sensorType;
// convert int to enum (sensorOf)
public void setSensor (int sensor)
{
sensorType = (sensorOf) sensor;
}
Your is much better, I don't need to make 2 enum, but how can I shorten this?
Follow up question: http://answers.unity3d.com/questions/1332521/how-to-shorten-calling-enum-value-from-other-scrip.html
if (sensorType == RespawnController.forceOf.ally)
to if (sensorType == respawn.forceOf.ally)
or if (sensorType == respawn.ally)