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. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice
  3. Join us on November 16th, 2023, between 1 pm and 9 pm CET for Ask the Experts Online on Discord and on Unity Discussions.
    Dismiss Notice

Parent and Child Class Question

Discussion in 'Scripting' started by IAmPatSilva, Dec 30, 2015.

  1. IAmPatSilva

    IAmPatSilva

    Joined:
    Dec 30, 2015
    Posts:
    7
    All I want is to get my player to retrieve the type of spell that is being cast.

    I have a parent class spell that has a member called type. Then I have another script who inherits that type and on start() it assigns the type to "Something". Parent class has a function getType which returns string type.

    Note that before I did this I had set the Parent's type to "poop", just to see if I would get it. It worked, however when I took that code out of the Parent's script and assigned the Child's type to something the game stayed retuning poop. So now, as funny as it is, I can't stop getting "poop". I want to get "Something"

    this is the parent class
    Code (CSharp):
    1. public class Spell : MonoBehaviour {
    2.  
    3.     protected string type;
    4.  
    5.     public virtual string getType()
    6.     {
    7.         return type;
    8.     }
    9.  
    10. }
    11.  
    this is the child class
    Code (CSharp):
    1. public class ChildSpell : Spell {
    2.    
    3.     public void Awake(){
    4.         type = "Something";
    5.     }
    6.  
    7.     public override string getType ()
    8.     {
    9.         return type;
    10.     }
    11. }
    my playerClass
    Code (CSharp):
    1. public class Mage : MonoBehaviour {
    2.  
    3.  
    4.     public Spell[] spellbook;
    5.  
    6.     public void Update(){
    7.  
    8.         if(Input.GetButtonDown("Spell1")){
    9.             Debug.Log(spellbook[0].getType());
    10.         }
    11.     }
    12.  
    13. }
    There is code that i took out of the playerClass because it doesn't have to do with the problem. But the ChildSpell was assigned to the Spell array via inspector. Again the problem is that it is still returning "poop" even though the code doesn't have it anymore.
     
  2. StarManta

    StarManta

    Joined:
    Oct 23, 2006
    Posts:
    8,744
    Put a Debug.Log in ChildSpell's Awake(), make sure that that is getting called.
     
  3. KelsoMRK

    KelsoMRK

    Joined:
    Jul 18, 2010
    Posts:
    5,539
    I believe you might have to make a virtual Awake method in your base class and override it in your child class. I vaguely remember some quirky behavior around that.
     
  4. IAmPatSilva

    IAmPatSilva

    Joined:
    Dec 30, 2015
    Posts:
    7
    Thanks for the help but it still didn't work. I think there is a problem with the way I am constructing my classes.
     
  5. IAmPatSilva

    IAmPatSilva

    Joined:
    Dec 30, 2015
    Posts:
    7
    I did this and it was it sending me back the message, so Its going through the right Awake but it is not actually setting the type to anything.
     
  6. IAmPatSilva

    IAmPatSilva

    Joined:
    Dec 30, 2015
    Posts:
    7
    *********
    * Fixed *
    *********
    All I want is to get my player to retrieve the type of spell that is being cast.

    I have a parent class spell that has a member called type. Then I have another script who inherits that type and on start() it assigns the type to "Something". Parent class has a function getType which returns string type.

    Note that before I did this I had set the Parent's type to "poop", just to see if I would get it. It worked, however when I took that code out of the Parent's script and assigned the Child's type to something the game stayed retuning poop. So now, as funny as it is, I can't stop getting "poop". I want to get "Something"


    I created a cast function as my init function
    ****

    this is the parent class
    Code (CSharp):
    1. public class Spell : MonoBehaviour {
    2.  
    3.     protected string type;
    4.  
    5.     public virtual string getType()
    6.     {
    7.         return type;
    8.     }
    9.  
    10.     public virtual string cast()
    11.     {/*mt*/}
    12.  
    13. }
    14.  
    this is the child class
    Code (CSharp):
    1. public class ChildSpell : Spell {
    2.  
    3.     public override string getType ()
    4.     {
    5.         return type;
    6.     }
    7.  
    8.     public override string cast()
    9.     {
    10.         type = "Something";
    11.     }
    12. }
    my playerClass
    Code (CSharp):
    1. public class Mage : MonoBehaviour {
    2.  
    3.  
    4.     public Spell[] spellbook;
    5.  
    6.     public void Update(){
    7.  
    8.         if(Input.GetButtonDown("Spell1")){
    9.             spellbook[0].cast();
    10.             Debug.Log(spellbook[0].getType());
    11.         }
    12.     }
    13.  
    14. }
    This code worked.