0

I feel like I am looking in too many directions at once. Basically, I have a trainer object/script who fights enemies with whichever animal she is currently handling. Therefore, the animal object might be one of many. I can easily assign the animal object itself, but I cannot figure out how to refer to the script of that animal object if I don't know in advance what animal she will be handling. For instance, the Lion object would have a lion script that has lion stats, and the dog object would have dog script, etc. An object can be directly assigned via the inspector panel, or given a tag, but I don't know how to refer to a script that you don't know the name of:

public class Player : MonoBehaviour {

    public GameObject Animal;

    int hpMax;
    int attack;
    int defense;
    int speed;
    int hpCurrent;
    string animalName;

    void Start () 
    {
        var animalObject = Animal.GetComponent<"AnimalScript">();

        //none of the following works
        hpMax = animalObject.hpMax;
        hpCurrent = hpMax;
        attack = animalObject.attack;
        defense = animalObject.defense;
        speed = animalObject.speed;
        animalName = animalObject.animalName;
    }
9
  • "refer to the script of that animal object" - What do you mean by this? What "script" are you talking about?
    – cbr
    Apr 16, 2015 at 20:04
  • 1
    You would most likely want to have a parent class or interface that the various animals inherit from, example: interface HostileAnimal{ void Attack(); } public class Lion{ public void Attack(){ "do work" } } public class Bear{ public void Attack(){ "do work" } } That way you have the same member to call from and the same method can have various effects; different damage, speed, etc. Does that make sense?
    – CalebB
    Apr 16, 2015 at 20:05
  • @GrawCube Each animal object has a script that refers to that animal's stats. The animal object itself is just an empty object, so the script is really the only thing I need, but you can't just refer directly to scripts as far as I know.
    – greyspace
    Apr 16, 2015 at 20:09
  • @calebB I know how to do this in C#, but I can't figure it out in unity. Would the parent class need to be attached to an object in the stack, or on a prefab?
    – greyspace
    Apr 16, 2015 at 20:13
  • That and when getting your GameObject, animalObject may be returned as a generic object or some other kind of object even though it is really a GameObject so you may have to cast the returned value. You can do something like this: GameObject animalObj = (GameObject)Animal.GetComponent<"...">(); hpMax = animalObj.hpMax;... Also as for @GrawCube's comment, script is a rather generic term within programming. Even if the object is called a script phrasing it like Script Object instead of script may help prevent confusion.
    – CalebB
    Apr 16, 2015 at 20:14

1 Answer 1

3

Have all of your animals inherit from a base class.

Animal.cs

public abstract class Animal : MonoBehaviour {

    public int hp;
    public int damage;
    //etc

    abstract public void doSomething();
}

Cat.cs

public class Cat : Animal {
    public override void doSomething() {
        Debug.Log("I'm meowing for food even though my bowl is full!");
    }
}

Then when referencing these classes from Player, just use the base type Animal.

public class Player : MonoBehaviour {

    public Animal myAnimal;

    void Start() {
        myAnimal.doSomething(); //meows
    }
}

In this example, I expect myAnimal to be assigned via inspector. To assign via code, do this:

myAnimal = someGameobject.GetComponent<Animal>();

The caveat of course is that all of your animals should have identical interfaces. You need to hide the implementation details of each animal inside of whatever base interface you define. There are numerous resources on how to do this and why it's good to do that. It's pretty important OOP stuff so it's good to learn.

And to address the concerns you have in the comments, Unity doesn't do anything weird and this should compile just fine (unless some of my syntax is bad). You should test things before assuming they won't work :)

5
  • It's not really a "super class" just a parent class. Ideally you'd implement a hierarchy of interfaces and classes. Just food for thought. :)
    – CalebB
    Apr 16, 2015 at 20:52
  • Sorry, that's my Java leaking into C#. Apr 16, 2015 at 20:53
  • @SirBraneDamuj: But the problem I am having is actually not solved by that. I can assign myAnimal from the inspector. Then I have access to CatObject via the code but not the access to Cat.Cs, which is where the functions you've described are actually located. Do I need to assign CatObject AND CatObject's attached Cat.CS file? Repeated for every animal? Likewise, if I write code that allows the player to select an AnimalObject, I then have to write code that refers to the Cs script (Cat.cs, Dog.cs) of whatever object that player chose. Do i need to write a huge list of if/then statements?
    – greyspace
    Apr 16, 2015 at 22:26
  • In the example: myAnimal = someGameobject.GetComponent<Animal>(); I know multiple ways of determing someGameobject, but no ways of determining GetComponent<Animal>
    – greyspace
    Apr 16, 2015 at 22:27
  • You don't need to determine anything, if you know the GameObject has some component that inherits from Animal, then you can just call that literally. Apr 17, 2015 at 3:32

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.