Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 14 Next capture
2021 2022 2023
2 captures
12 Jun 22 - 14 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 Ultimate360 · Mar 19, 2017 at 06:53 PM · prefab-instanceparameterpassingmanipulationenumerate

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!

alt text

enum-passtopublicmethod.png (36.3 kB)
Comment
Add comment
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

1 Reply

· Add your reply
  • Sort: 
avatar image
1

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;
 }
Comment
Add comment · Show 1 · 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 Ultimate360 · Mar 29, 2017 at 04:17 AM 0
Share

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)

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

4 People are following this question.

avatar image avatar image avatar image avatar image

Related Questions

Could I make a 3d object shaper tool in unity 1 Answer

Noob problem: Unable to manipulate object after adding MRTK scripts 0 Answers

return value of a function passed to another function as a parameter 0 Answers

Can't change parameters on Blend Tree 1 Answer

How to pass parameters to an object with an object pooling system? 0 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