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 /
This question was closed Oct 17, 2019 at 04:43 AM by kevenn for the following reason:

Too subjective and argumentative

avatar image
0
Question by kevenn · Oct 16, 2019 at 02:12 PM · variablesinheritancepolymorphism

How to increase a variable for individual prefab when using inheritance

I recently learned about inheritence and polymorphsm. If I'm able to individually modify their stats with this method how would i upgrade their stats if say i had 2 prefabs? For example.

/

Lets say i have a super class to contain my variables called SuperSpell

/

 public abstract class SuperSpell : MonoBehaviour
 {
     public string NAME;
     public int LEVEL;
     public int COST;
     public int OUTPUT_GOLD;
     public int OUTPUT_SPELL;
     public float ATK;
     public float DURATION; //60 Seconds
 }

/

Now I have a child class called BTN_Spell_Controller. This script is inheriting SuperSpell and 2 prefabs are using this script. I'm able to individually modify their stats in the inspector.

/

alt text

/

For BTN_Spell_controller I added static variables to store the inherited variable for the prefabs.

/

 public class BTN_Spell_Controller : SuperSpell
 {
     public static int level;
     public static int gold_output;
     public static int spell_output;
     public static float duration;
     public static float attack;
 
     private void Start()
     {
         level = LEVEL;
         gold_output = OUTPUT_GOLD;
         spell_output = OUTPUT_SPELL;
         duration = DURATION;
         attack = ATK;
     }
 
     // Use this for initialization
     void Start()
     {
 
     }

/

This part is where i get confused. Finally I have a third script called BTN_UI_UPGRADE used to upgrade my one of my prefab's inherited variables through a button press. Note this class is inheriting BTN_UI_UPGRADE and grabbing the static classes (Note sure if thats a bad idea)

/

 public class BTN_UI_UPGRADE : BTN_Spell_Controller
 {
     public void Add()
     {
         gold_output += 1;
     }
 }

/

The problem is I want the upgrade to affect only one of my prefabs and I know this affects all of them. I'm unsure what strategy to use in this case. Sorry if my explanation was too long or not enough info. Thanks!

capture2.png (9.3 kB)
Comment
Add comment · Show 1
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 Bonfire-Boy · Oct 16, 2019 at 05:22 PM 0
Share

I can't make head or tail of your use of static variables here. It's clearly wrong, but I've no idea what it's trying to achieve. Why don't you just leave all that out. Your subclass inherits all the fields in its base class, you don't need to copy them into the subclass... just use them.

Also, you say "grabbing the static classes" but there are no static classes here.

I think you need to read up on the use of the static keyword. There are plenty of resources if you google "static keyword c#" but also plenty of answers on here that already explain it.

1 Reply

  • Sort: 
avatar image
-1

Answer by HappiiGamer · Oct 16, 2019 at 02:22 PM

I just noticed that your gold_ouput is static!? I wrote my first answer thinking it not to be static.


Make your fields non static, and look up what is static.

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 Bonfire-Boy · Oct 16, 2019 at 05:23 PM 0
Share

This is wrong. gold_output is static, so if you change its value then yes, you do change its value for all instances.

avatar image HappiiGamer Bonfire-Boy · Oct 16, 2019 at 05:42 PM 0
Share

Yea, I missed that it was static.

Follow this Question

Answers Answers and Comments

117 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 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 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 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 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 avatar image avatar image

Related Questions

An OS design issue: File types associated with their appropriate programs 1 Answer

How to change variables from another script? 1 Answer

How can I access an inherited method from a separate (collided) object? 1 Answer

Calling methods on inherited classes in C# 2 Answers

C# Inheritance - Reference the INSTANCE of a variable, is it possible? 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