Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 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 /
avatar image
0
Question by grechzoo · Mar 16, 2014 at 05:49 PM · inheritanceparent-child

inheriting a class, but wanting it to instantiate on each gameobject it's applied to?

Basically I have a parent class script, called Card which has all the variables and logic in the methods, that will apply to a set of ten cards.

 public class Card : MonoBehaviour {

 
     public Texture MainTexture;
     public Texture HoverTexture;
  
     public int health;
     public string cardName;
       public bool selected;
     public bool hover;
     public virtual void Start () 
     {
         selected = false;
         hover = false;
         health  = 50;
     }

     public virtual void Update () 
     {
         ray = Camera.main.ScreenPointToRay(Input.mousePosition);
         if (Physics.Raycast(ray, out hit))
         {
             if (selected != true)
                 hover = true;
 
             if (Input.GetMouseButtonDown(0))
             {
                 if (selected)
                 {
                     selected = false;
                     hover = true;
                 }
             }
         }
     }
 
 }

note: the code is pared down to save room, I for example have extra logic to make sure hover is put to false when not over, etc. so there's not problem there.

Then I have a child class that will represent each of the individual cards, such as fire and aquatic, so I can change things like their names from within this child class.

 public class CardAquatic : Card {
 
     public override void Start () 
     {
         base.Start();
         cardName = "aquatic";
     }
     public override void Update () 
     {
         base.Update();
     }


I then add this child script to the game object card (one for fire and one for aquatic) this allows me access to all of the variables in the Card parent class on the inspector so I can assign the specific textures and track the values.

However when I start the game, both cards act as one. (they always share the same hovered or select status :( )

I imagine its because both their parents is simply one class and is seen as one single instance, even though I have created two separate children of that class, and was hoping for them to have individual individual inherited variables.

So is there a way I can make this work? Without sacrificing the useful aspect of having the parent have all the common logic and methods that all cards will use, but have each instance of the child be individual and not share the same parent variables.

As this method allows me to change the overall common logic of all cards, without having to copy and paste to each individual card (there will be 10+)

Hope you can help!!!

Comment
Add comment · Show 6
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 jbecana · Mar 16, 2014 at 07:08 PM 0
Share

Inheritance shouldn't be a problem.I don't get what should make the difference in your children cards?

avatar image jbecana · Mar 16, 2014 at 09:44 PM 0
Share

What is selected for and where is set to true? Again, inheritance is not the problem, there should be something wrong in the logic.

avatar image grechzoo · Mar 16, 2014 at 10:50 PM 0
Share

selected and hovered are public booleans defined in the Card parent class.

hover is set to true when the mouse is hovered over (and false when hovered away) selected is when the user clicks while hovered is true. and disabled when clicked again.

here is the full method, just in case there is something wrong that I really didn't see before. (shortened it in the OP cause i didn't think it was the problem)

Truly apologise if the error is in here, and i didn;t think to include it all :(,

 public class Card : $$anonymous$$onoBehaviour {
 
     Ray ray;
     RaycastHit hit;
 
     public Texture $$anonymous$$ainTexture;
     public Texture HoverTexture;
     public Texture SelectTexture;
 
     public int health;
     public int attack;
     public string cardName;
     public string weakTo;
     public string strongTo;
 
     public bool selected;
     public bool hover;
 
     // Use this for initialization
     public virtual void Start () 
     {
         selected = false;
         hover = false;
         health  = 50;
     }
     
     // Update is called once per frame
     public virtual void Update () 
     {
         ray = Camera.main.ScreenPointToRay(Input.mousePosition);
         if (Physics.Raycast(ray, out hit))
         {
             if (selected != true)
                 hover = true;
 
             if (Input.Get$$anonymous$$ouseButtonDown(0))
             {
                 if (selected)
                 {
                     selected = false;
                     hover = true;
                 }
                 else
                 {
                     selected = true;
                     hover = false;
                 }
             }
         }
         else
         {
             hover = false;
         }
 
         Draw();
     }
 
     void Draw()
     {
         if (hover)
         {
             renderer.material.mainTexture = HoverTexture;
         }
         else if (selected)
         {
             renderer.material.mainTexture = SelectTexture;
         }
         else
         {
             renderer.material.mainTexture = $$anonymous$$ainTexture;
         }
     }
 
 }

as always thanks so much for giving your time to trying to help me, and i apologise if im being dumb.

avatar image jbecana · Mar 16, 2014 at 11:13 PM 0
Share

You should move that raycast to another script that runs once - right now you are doing as much raycastings as cards you've got - and next, you should check that raycast to see what collider is hitting - right now whenever it hits something it turns true, that's the reason why all cards show hover.

avatar image grechzoo · Mar 17, 2014 at 01:02 AM 0
Share

Thanks so much for the advice, that sounds like it will solve the problem. :)

ill work out the solution and post back here.

Show more comments

2 Replies

· Add your reply
  • Sort: 
avatar image
0

Answer by wildex999 · Mar 16, 2014 at 06:14 PM

Inheriting from a base class should not make two instances share the same values on variables, unless the field is static.

Can you share the code where you instantiate the cards? Sounds like you keep two references to the same instance.

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 grechzoo · Mar 16, 2014 at 09:22 PM 0
Share

The only other script in the project is CardParent Which simply compiles all the child transofrms under an empty object called "All Cards". so the script on this empty object which holds as its children the cards is the following.

 public class CardParent : $$anonymous$$onoBehaviour {
 
     public List<Transform> CardList = new List<Transform>();
  
     void Start () 
     {
         foreach(Transform card in this.transform)
         {
             CardList.Add(card);
         }
     }
 
     void Update () 
     {
     }
 }

Here is an image of one of the cards in the interface to understand how I added the CardAquatic script which is the second part of the code on my original post, that inherits from the Card Class overall.

Inspector Image

And here's an image in game showing that with Aquatic selected in the inspector, the mouse is actually over Fire, yet aquatic stlil shows as hover being true. showing that the two cards are being treated as one card overall, ins$$anonymous$$d of two seperate instances.

In Game Image

Again any more suggestion would be massively appreciated.

Thanks.

first image.jpg (435.5 kB)
in game.jpg (436.2 kB)
avatar image
0
Wiki

Answer by jbecana · Mar 17, 2014 at 06:57 AM

I would create a Player() script and put the card selection code in its Update():

        // Select card
        ray = Camera.main.ScreenPointToRay(Input.mousePosition);
        if (Physics.Raycast(ray, out hit, 1 << CardLayer))
        {
          // Only cards in CardsLayer
          // Take advantage of inheritance in next lines
          Card myCard = hit.transform.GetComponent<Card>();
          if (myCard is CardAquatic)
          {
             //doCardAquaticTask
          }
          else if ((myCard is CardXXXX)
          {
             //doCardXXXTask
          }

Using a layer would assure that you are only selecting card types, but it's not strictly required because you're checking for a card component next.

Comment
Add comment · Show 2 · 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 Jamora · Mar 17, 2014 at 01:20 PM 0
Share

Ins$$anonymous$$d of rayvasting, the On$$anonymous$$ouseDown function should be placed in the Card script. I find it to be more readable.

avatar image jbecana · Mar 17, 2014 at 01:42 PM 0
Share

Sure, On$$anonymous$$ouseDown should be in the Card script. Readability may depend on your design, some people prefer keep player inputs centralized in one script. Anyway, I think your approach may integrate easier in this case.

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

23 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

A node in a childnode? 1 Answer

I have two buttons in my c# script, i want to make invissible one button and make it vissible on click of another button at runtime. Can anyone please show me the way for the proper solution. 1 Answer

Issues using the 'new' keyword 3 Answers

Sprites keep overlapping 1 Answer

Please anyone know about ads banners.. how do we implement in the unity3d free version... 1 Answer


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