Inheritance problem
Hi, i am new of using unity. I have a simple problem about inheritance . Please help me. I have 3 scripts about this problem.
Controller script:
...do something to set selectedPlayer and selectedAbility, and call "useAbility" ...
public void useAbility (GameObject target)
{
selectedAbility.Getcomponent<FireBall> ().Target = target;
Instantiate (selectedAbility, selectedPlayer.transform.position, selectedPlayer.transform.rotation);
}
Fireball script:
public class FireBall : Ability
{
bool end;
void Update()
{
if (!end) Action();
}
void Action ()
{
Debug.Log(Target == null); //return Ture, Why!?!?
}
}
Ability script:
public class Ability : MonoBehaviour
{
GameObject caster;
GameObject target;
public GameObject Caster
{
get { return caster; }
set { caster = value; }
}
public GameObject Target
{
get { return target; }
set { target = value; }
}
}
The fireball game object is instantiate sucessful. so i can see that. but I don't know why the "Target" in "Fireball" script return null.
Answer by M-Hanssen · May 12, 2016 at 02:04 PM
First of all, use auto properties for your over complicated construction inside the "Ability" class:
public class Ability : MonoBehaviour
{
public GameObject Caster { get; set; }
public GameObject Target { get; set; }
}
Correct me if I'm wrong, but I think what you are trying to do is this:
public class Controller : MonoBehaviour
{
// Make sure the AbilityPrefab is filled with the prefab to instantiate inside the inspector!
public Ability AbilityPrefab;
private Ability selectedAbility;
public void UseAbility(GameObject target)
{
selectedAbility = ((GameObject)Instantiate(AbilityPrefab, selectedPlayer.transform.position, selectedPlayer.transform.rotation)).GetComponent<Ability>();
selectedAbility.GetComponent<FireBall>().Target = target;
}
}
public class Ability : MonoBehaviour
{
public GameObject Caster { get; set; }
public GameObject Target { get; set; }
}
public class FireBall : Ability
{
private bool end;
protected void Update()
{
if (!end) Action();
}
protected void Action()
{
Debug.Log(Target == null); //return Ture, Why!?!?
}
}
Yes, its work, my idea is that.
Don't know why i use same method to set "Caster" in "readyAbility()" then work, but it's not work in "useAbility()".
GameObject selectedAbility; //Prefab
GameObject selectedPlayer;
bool usingAbility;
****first, user selected player and show UI button of ability****
public void readyAbility() //UI Button clicked
{
usingAbility = true;
selectedAbility = selectedPlayer.GetComponent<player_ability> (). ability; //get prefab from player
selectedAbility.GetComponent<Ability> (). Caster = selectedPlayer; // its work
}
public void UseAbility(GameObject target) //user selected enemy after click button
{
//not work
//selectedAbility.Getcomponent<FireBall> ().Target = target;
//Instantiate (selectedAbility, selectedPlayer.transform.position, selectedPlayer.transform.rotation);
//its work
GameObject ab = (GameObject)Instantiate(selectedAbility , selectedPlayer.transform.position, selectedPlayer.transform.rotation);
ab.GetComponent<Ability>().Target = target;
usingAbility = false;
}
Your answer
![](https://koobas.hobune.stream/wayback/20220612080639im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
Why Would Trie Data Retrieval Work in Windows Build but not Mac/HTML? 0 Answers
pick up object using a key and then playing a sound 0 Answers
I cant figure out these errors 0 Answers
Grapling gun help! Why doesn't my player move towards? Instead of getting stuck in place 0 Answers
Camera Issue,Camera is changing it's position in game view. 0 Answers