- Home /
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!
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.
Thanks for the response, but i dont really understand.
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.
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
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'
}
}
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.
Your answer
Follow this Question
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