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

Inheritance Question...

Discussion in 'Scripting' started by oglepole, Dec 4, 2014.

  1. oglepole

    oglepole

    Joined:
    Jan 25, 2014
    Posts:
    17
    I am hoping someone can lead me in the right direction - as I feel like I just don't understand what I did wrong here.

    I have attached 3 simple classes below - I am trying to inherit different abilities from a base class called monsterAbilities. PoisonClaw would be one of these. Inside of my third class, zombie - I made a list for monsterAbilities. Inside the unity editor I simply dragged/dropped the PoisonClaw prefab I made (its an empty game object with the poisonclaw script on it) into the first slot of the List.

    I am confused because when I run this code...I keep getting back a result of false...Why isn't this coming back true? What did I miss - I feel like its got to be something very obvious.

    Code (csharp):
    1.  
    2. public abstract class monsterAbilities : MonoBehaviour
    3. {
    4.  
    5.     public bool abilityReady;
    6.  
    7.     // Use this for initialization
    8.     public virtual void Start ()
    9.     {
    10.  
    11.     }
    12.  
    13.     // Update is called once per frame
    14.     public virtual void Update ()
    15.     {
    16.  
    17.     }
    18.  
    19. }
    20.  
    Code (csharp):
    1.  
    2. public class PoisonClaw : monsterAbilities
    3. {
    4.  
    5.     public override void Start ()
    6.     {
    7.         abilityReady = true;
    8.     }
    9.  
    10.     public override void Update()
    11.     {
    12.    
    13.         Debug.Log(abilityReady);
    14.         //Returns True
    15.     }
    16.  
    17. }
    18.  
    Code (csharp):
    1.  
    2. public class Enemy_Basic_Zombie_AI : MonoBehavior
    3. {
    4.     public List<monsterAbilities> monsterAbil = new List<monsterAbilities>();
    5.  
    6.     public void Update()
    7.     {
    8.    
    9.         Debug.Log(monsterAbil[0].abilityReady);
    10.         //Returns False
    11.     }
    12. }
    13.  
     
  2. OpticalOverride

    OpticalOverride

    Joined:
    Jan 13, 2013
    Posts:
    161
    Does the monsterAbil list contain an instance of the monsterAbilities component? Looks like you may have forgotten to add the component. Try adding this to Enemy_Basic_Zombie_AI:

    Code (CSharp):
    1. void Start()
    2. {
    3.     monsterAbil.Add( GetComponent<monsterAbilities>() );
    4. }
    Or something like that at least if you haven't already populated the list. Make sure both the Enemy_Basic_Zombie_AI and PoisonClaw components are attached to the zombie enemy game object, in this case.
     
  3. oglepole

    oglepole

    Joined:
    Jan 25, 2014
    Posts:
    17
    So instead of doing GetComponenet and such, I literally dragged a prefab call PoisonClaw (Its a gameobject that just contains a single PoisonClaw script) into the list in the editor. Is that my problem? Am I not allowed to do it like that?
     
  4. oglepole

    oglepole

    Joined:
    Jan 25, 2014
    Posts:
    17
    So i tried doing a GetComponent instead...and to no avail. It still returns false. Anyone have any other ideas where I went wrong?
     
  5. Suddoha

    Suddoha

    Joined:
    Nov 9, 2013
    Posts:
    2,824
    Try to assign an instance of the prefab and not the prefab itself.
     
    oglepole likes this.
  6. OpticalOverride

    OpticalOverride

    Joined:
    Jan 13, 2013
    Posts:
    161
    Exactly, make sure the prefab is a game object in the scene, then you can drag it into the list (didn't realize the list was public initially).
     
    oglepole likes this.
  7. oglepole

    oglepole

    Joined:
    Jan 25, 2014
    Posts:
    17
    That worked... I guess im just confused. Whats the difference between me dragging my prefab object into the scene before hand, and me just dragging the prefab into the List. Weird.
     
  8. OpticalOverride

    OpticalOverride

    Joined:
    Jan 13, 2013
    Posts:
    161
    Because the prefab needs to be instantiated first. By dragging the prefab into the scene, you have effectively created an instance of that component. When dragging straight from the project view, think of the component as like a "ghost"; it doesn't really exist yet as far as Unity is concerned, it is simply a template (or prefab) of a possible game object. So when you go to get some data about this "ghostly" component, it is just garbage nonsense (the bool you were trying to read from in the component likely didn't have a location in memory yet, as the component was not properly instantiated in the scene), and therefore you were getting false, or not what you expected. Again, by dragging this prefabricated game object into the scene first, it was properly created, with memory locations and all.

    Hope this makes sense.