Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 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 /
  • Help Room /
avatar image
0
Question by cinan032 · May 12, 2016 at 01:54 PM · programming

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.

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
0
Best Answer

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!?!?
     }
 }

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 cinan032 · May 13, 2016 at 05:35 AM 0
Share

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

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

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


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