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 Kazuma17 · Apr 11, 2020 at 12:41 PM · accessing scriptsinherited-members

Accessing Inherited variable before it instanciation

Hello all ! Sorry if my english is bad, i'm not a native :)

I have a class Enemy.cs, then each type of enemy got his own script which derives from Enemy.cs, so I can override functions like Shot(), Move(), etc. I got a boolean "isBig", because for spawning purpose, some enemies have twice the size of others. With parameters in a function that is not shown here to simplify, I can spawn different enemies in the same time but I can't have more than 30 enemies that pop in the same time, so I need to calculate before instantiate them if my parameters are right. You have more explanations on the code annotations.

So if my conception is good, my question is : How can I ask the isBig variable value of the derived class of Enemy.cs before even instantiate them ? Is there a way to store data that came from a parent class and is override before the Game is started ? I don't know if I am clear so don't hesitate to ask question.

Here are my scripts : The parent class Enemy.cs, where i wrote the base mechanics for all enemies and avoid duplicate code :

    public class Enemy : MonoBehaviour
     {
         protected float _speed = 4.0f;
         protected float _fireRate = 2.0f;
         protected int _health = 1;
         protected int _damage = 5;
         protected int _score = 10;
         protected bool _isBig;
     
         protected Player _player;
     
         protected virtual void Start()
         {
             InitializeEnemy();
         }
     
         void InitializeEnemy()
         {
             _player = GameObject.Find("Player").GetComponent<Player>();
     
             if (_player == null)
             {
                 Debug.LogError("Player is NULL");
             }
         }
     
     public bool isBig()
         {
             return _isBig;
         }
 [...]
 }

SpawnManager where I want to have access to the "isBig" boolean of my child class :

 public class SpawnManager : MonoBehaviour
 {
     [SerializeField]
         private Enemy[] _enemiesPrefab;
         [SerializeField]
         private Transform[] _spawnPositions;
     
      void Start()
         {
     // Exemple where I have FALSE instead of TRUE, because my ennemy is not instantiated so it takes the ISBIG variable from the parent class, and I want the isBig from the enemy that I indicate. Of course I don't know in advance if it will be a big enemy or not (and so take the right script type directly) but this is to demonstrate that even if i know that, it will not work. 
     
             Big_Enemy bigEnemyScript = _enemiesPrefab[1].GetComponent<Big_Enemy>();
             Debug.Log(bigEnemyScript.isBig());
     
           //Here is the real purpose of this question, I pass in parameters that I want instantiate _enemiesPrefab[0] 25 times and _enemiesPrefab[1] 3 times. Since index 0 is an enemy which count for 1 size, and index 1 is an enemy that is 2 size ("a big enemy"), result will be 25x1 + 3x2 = 31. Instead of this, I got 28 because of 25x1 + 3x1 cause the isBig of the big enemy is take from his parent and not from him... 
             
      Debug.Log(CheckTotalEnemies(0, 25, 1, 3));
         }
     
     private bool CheckTotalEnemies(int firstEnemyType, int firstEnemyNumber, int secondEnemyType = -1, int secondEnemyNumber = 0, int thirdEnemyType = -1, int thirdEnemyNumber = 0)
         {
             int total = 0;
 
           // I multiply by two if it's a big enemy then I add to the total, or I directly add to the total if it's a 1 size enemy.
             total = _enemiesPrefab[firstEnemyType].isBig() ? total += firstEnemyNumber * 2 : total += firstEnemyNumber;
     
          // Add a second, or a third enemy type is optional
             if (secondEnemyType != -1)
             {
                 total = _enemiesPrefab[secondEnemyType].isBig() ? total += secondEnemyNumber * 2 : total += secondEnemyNumber;
     
                 if (thirdEnemyType != -1)
                     total = _enemiesPrefab[thirdEnemyType].isBig() ? total += thirdEnemyNumber * 2 : total += thirdEnemyNumber;
             }
            
          // I check if the total doesn't exceed the maximum enemy size which is 30
             return total <= 30 ? true : false;
         }
 }

And this the script of a big enemy which inherit from Enemy.cs :

 public class Big_Enemy : Enemy
 {
     protected override void Start()
     {
         base.Start();
         InitializeDefaultValue();
     }
 
     void InitializeDefaultValue()
     {
         _speed = 1.0f;
         _fireRate = 10f;
         _health = 1;
         _damage = 20;
         _score = 10;   
         _isBig = true;
     }
 }

The script of a simple enemy is the same without the IsBig line.

Thank you very much for any help ! :)

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

Answer by Kazuma17 · Apr 12, 2020 at 06:40 AM

Up please :)

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 Kazuma17 · Apr 13, 2020 at 09:13 AM 0
Share

Please, any help is good to take ! :)

To resume my problem in a few lines : I got derived class (polymorphism) that change a parent value variable at instanciation. The problem is that I want to create a spawning editor, and I need to know before any instanciation the value of this variable in the child class. Since this value in the child class is changed in it instanciation, to my editor it value is always the value of the parent class. Is there a way that without instanciation, to attribute an inherit value to a derived class ?

Thank you !

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

196 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 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

Cant Access Function in Other Script C# 3 Answers

UI Button Fade Duration 1 Answer

Reference coroutine from another script 2 Answers

[Beginner] C# PauseMenu script can't access the scripts from my FirstPersonController (C# & JS)? 1 Answer

Identify Variable from Triggering Object 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