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 jetty112 · Jun 01, 2014 at 01:33 PM · variableparentclassinheritancestructure

How to build up the class structure for an object

Hi!

So i have this character "Soldier" and this gameobject has a script called "Soldier". In this script i declare a set of variables for the class.

I also have another script on the gameobject called "SoldierMove" and this script/class inherits from the "Soldier" script/class.

However when i set a prefab for the "Soldier" classes variable "wayPointTarget", i cant seem to access this in the "SoldierMove" script.

Also in the "Soldier" scipt when i set the variable currentTarget, in the script "SoldierMove" it stays null.

Isnt the "SoldierMove" script supposed to inherit the values from the "Soldier" script?

Here is some of the "Soldier" script:

 using UnityEngine;
 using System.Collections;
 
 public class Soldier : MonoBehaviour {
 
     
     public Transform currentTarget;
     public GameObject wayPointTarget;
 
     // Use this for initialization
     void Awake() {
         currentTarget = GameObject.Find ("Target");
     }
     
     // Update is called once per frame
     void Update () {
     
     }
 }

And Here is the "SoldierMove" script:

 using UnityEngine;
 using System.Collections;
 
 public class SoldierMove : Soldier {
     
     // Use this for initialization
     void Start () {
         Instantiate (wayPointTarget, Vector3(0, 0, 0), Quaternion(0, 0, 0));
     }
     
     // Update is called once per frame
     void Update () {
         Debug.Log (currentTarget.name);
     }
 }

Note that these are not the actual scripts, but rather conceptual scripts meant to illustrate the problem.

Why i want to do all of this is so taht i dont have to have one large script (the "Soldier" script), but rather a set of smaller scripts, that each do a specific thing, like move, attack, find target and so on. And i want to access the variables easily, so if i for example set in the "FindTarget" script the "currentTarget" variable to be "Target", then in the "Attack" script i want to access that variable and know what the target currently is.

So am i missunderstanding the inheritance system, or what am i doing wrong?

Thanks in advance!

Comment
Add comment · Show 3
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 NoseKills · Jun 01, 2014 at 02:04 PM 1
Share

This is the one thing that's "wrong" with platforms like Adobe Flash IDE and Unity, that try to simplify program$$anonymous$$g; They can sometimes be very misleading in terms of what is actually happening on the programmatical level (because mos of the time you don't have to care about it).

When you drag scripts into a GameObject, each of those scripts become an instance of that class. So when you have a Soldier script and Soldier$$anonymous$$ove script in a GameObject, it's basically the same as saying

 Soldier soldierInsance = new Soldier();
 Soldier$$anonymous$$ove soldier$$anonymous$$oveInstance = new Soldier$$anonymous$$ove();

Even if Soldier$$anonymous$$ove inherits Soldier, these are 2 separate instances and they both have their own variables.

Because of the inheritance though, Soldier$$anonymous$$ove class is able to do everything that Soldier class does (and more), so you should only give the Soldier$$anonymous$$ove script to your GameObject and then do everything through that.

The other option would be to break the inheritance and give Soldier class a variable with a reference to the GameObject's Soldier$$anonymous$$ove script.

avatar image jetty112 · Jun 01, 2014 at 04:30 PM 0
Share

Thanks for the response, but i dont really understand.

  1. Because of the inheritance though, Soldier$$anonymous$$ove class is able to do everything that Soldier class does (and more), so you should only give the Soldier$$anonymous$$ove script to your GameObject and then do everything through that. If i only gave the GameObject the Soldier$$anonymous$$ove script, then i still couldnt create several small scripts ins$$anonymous$$d of one massive one.

  2. The other option would be to break the inheritance and give Soldier class a variable with a reference to the GameObject's Soldier$$anonymous$$ove script. How would this work. Can you give a code example please?

Thanks

avatar image NoseKills · Jun 01, 2014 at 05:55 PM 0
Share

1) Seems like you could benefit from doing some inheritance tutorial :) The most natural case for using inheritance is a situation such as making a base class Bird (Soldier) and subclasses Emu and Eagle (Soldier$$anonymous$$ove) that inherit it. Write everything a bird can do in the Bird class, and then implement the differences in the subclasses (Emu walks, Eagle flies)

If you want to make a GameObject behave like an Emu, just drag the Emu scrip into it. Now it can do everything an Emu can and all that a Bird can, because an Emu is a Bird.

Because of inheritance your Soldier$$anonymous$$ove class contains all that you have written into the Soldier class, that's what i meant.

2) What i meant is you could have the scripts without the inheritance and make them interact as you want by givig each a reference to the other

 public class Soldier : $$anonymous$$onoBehaviour
 {
     protected Soldier$$anonymous$$ove _$$anonymous$$over;
 
     void Start()
     {
         _$$anonymous$$over = GetComponent<Soldier$$anonymous$$ove>();
     }
 
     void updateAI()
     {
         _$$anonymous$$over.move(findNearestEnemy());
     }
 
     Vector3 findNearestEnemy()
     {
         // return the position of nearest enemy
     }
 }
 
 public class Soldier$$anonymous$$ove : $$anonymous$$onoBehaviour
 {
     protected Soldier _Soldier;
 
     void Start()
     {
         _Soldier = GetComponent<Soldier>();
     }
 
     public void move(Vector3 v)
     {
         // do the pathfinding & walking animation from here to 'v'
     }
 }
 
 

1 Reply

· Add your reply
  • Sort: 
avatar image
0

Answer by _sh4na_ · Jun 01, 2014 at 08:19 PM

When you have a gameobject with two scripts attached with it, each script is going be instantiated as a new object. The code will do new Soldier() and new SoldierMove() and attach those two instances to the gameobject. When you set something on one of them, the other one is not going to know about it because it's a different instance (regardless of inheritance).

It's perfectly acceptable to have multiple scripts in one gameobject because it gives you the ability to query the gameobject that the scripts are attached to for its components (i.e., other scripts), so from the SoldierMove script you can do GetComponent(), which will return the actual instance of the Soldier script. From that you can access the target variable and do what you need.

Comment
Add comment · 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

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

Is it possible to set variables for classes? 2 Answers

Parent Class variables in Child Class's Inspector (C#) 0 Answers

Putting a Child Class into a List of Parent Type and then casting it back to a Child 0 Answers

How can I redefine attributes of a parent in the child class? 1 Answer

Private variable is not saved 3 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