Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. Dismiss Notice

HELP: Public Enums vs Inheritance/Remote Call

Discussion in 'Scripting' started by Tom-Red, Jul 12, 2014.

  1. Tom-Red

    Tom-Red

    Joined:
    Jun 9, 2013
    Posts:
    15
    Greetings!

    So my issue is composed of two parts; The first being a public Enumerator containing three values that I wish to be able to change in the editor. (Meaning that the enumerator must stay public)
    The second part of my issue is that I wish to call a Public function from a different script, which will just do something depending on which Enumerator is selected in the editor.

    SCRIPT 1:
    Code (CSharp):
    1. public class Interactable : MonoBehaviour
    2. {
    3.     public InteractableType interactableType;
    4.     public enum InteractableType{
    5.         NPC,
    6.         Enemy,
    7.         Destructible
    8.     }
    9.  
    10. public void setList()
    11.     {
    12.         if(interactableType == InteractableType.NPC)
    13.         {
    14.             foreach(string s in npc)
    15.                 buttons.Add (s);
    16.         }
    17.         else if(interactableType == InteractableType.Enemy)
    18.         {
    19.             print ("Is enemy.");
    20.  
    21.             foreach(string s in enemy)
    22.                 buttons.Add(s);
    23.         }
    24.         else if(interactableType == InteractableType.Destructible)
    25.         {
    26.             foreach(string s in destructible)
    27.                 buttons.Add(s);
    28.         }
    29.     }
    The intention here is to call setList from somewhere else, and do something depending on the InteractableType selected in the editor.

    SCRIPT 2:
    Code (CSharp):
    1.    
    2. public class Interaction : Interactable
    3. {
    4.  
    5. void Interact()
    6.     {
    7.         Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);
    8.  
    9.         if(Physics.Raycast(ray, out hit))
    10.         {
    11.             if(hit.transform.gameObject.tag == "Interactable")
    12.             {
    13.                 setList();
    14.             }
    15.         }
    16.     }
    This simply calls the function "setList" inherited from the class "Interaction".



    That is what I expect should happen anyway, but the results I have gotten from debugging this setup is:
    When "setList" is called remotely, the value of "interactableType" is always the first selection in the original Enumerator, which in this example is "NPC".
    Code (CSharp):
    1.     public enum InteractableType{
    2.         >>>>>NPC,
    3.         Enemy,
    4.         Destructible
    5.     }
    However, if "setList" is called from within the same script as the Enumerator, the feature works as intended.

    Does anyone have a solution and a layman's explanation for this phenomenon? It would be much appreciated!
     
  2. LightStriker

    LightStriker

    Joined:
    Aug 3, 2013
    Posts:
    2,716
    How is it called remotely?
     
  3. Tom-Red

    Tom-Red

    Joined:
    Jun 9, 2013
    Posts:
    15
    By that I mean called from outside the script. i.e either from inheritance, or by creating an instance of the script and calling it that way.
     
  4. LightStriker

    LightStriker

    Joined:
    Aug 3, 2013
    Posts:
    2,716
    Well, your problem is from external call, so obviously your error is how you make your instance or how you call it. Start explaining if you want help. :p
     
  5. Tom-Red

    Tom-Red

    Joined:
    Jun 9, 2013
    Posts:
    15
    There are examples in my post of how my setup is like; One script has a function called "setList" that I am calling from an inherited script by simply using "setList();", as you can clearly see in my examples.

    What happens is that the function is called, but the if-statements are ignored and it seems to somehow always default into the first option in the original Enumerator, being "NPC" if I debug the "interactableType" variable. However, it will only do this if it is called from an inheritance or with an instance. If the function is called in the Update function of the same script that the Enumerator is in, the if-statements work as intended.
     
  6. Tom-Red

    Tom-Red

    Joined:
    Jun 9, 2013
    Posts:
    15
    By the help of some friends, I have found the solution to the problem. The external call needed to know exactly which object was the target of the call, so by creating an instance of the Interactable script and setting it to "hit.collider.gameObject.GetComponent<Interactable>();" then calling it this way: "interactable.setList();", made it work.

    Code (CSharp):
    1.    
    2. public class Interaction : Interactable
    3. {
    4.  
    5. private Interactable interactable;
    6.  
    7. void Interact()
    8.     {
    9.         Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);
    10.  
    11.         if(Physics.Raycast(ray, out hit))
    12.         {
    13.             if(hit.transform.gameObject.tag == "Interactable")
    14.             {
    15.                 interactable = hit.collider.gameObject.GetComponent<Interactable>();
    16.                 interactable.setList();
    17.             }
    18.         }
    19.     }
    Odd that I didn't get any help from the forums though, this turned out to be a pretty simple solution and my issue was explained well enough.
     
  7. LightStriker

    LightStriker

    Joined:
    Aug 3, 2013
    Posts:
    2,716
    o_O Really? I explicitly asked you how you were calling your instance, and you didn't post that code.
     
  8. Tom-Red

    Tom-Red

    Joined:
    Jun 9, 2013
    Posts:
    15
    I didn't post that code, because it didn't exist before. What I did before this was simply trying to call it by inheritance, not by an instance. I even explained that. See SCRIPT 2.