Varibles for Data types?

Hi everyone,

I wan’t to make a script that will determine what data type to use.

basically i have a player and a enemy game objects, i want to put this script on both of them. then have a if statment like:

public X scriptOmega;
        void Start() {
            if (this.gameObject.tag == "Player") {
                scriptOmega = GetComponent<PlayerScript>();
            }
            if (this.gameObject.tag == "Enemy") {
                scriptOmega = GetComponent<EnemyScript>();
            }
   
    }

So the data type(X) will change to the corresponding script(PlayerScript or EnemyScript).

is there method for this?

X can be any type that is a common ancestor of both PlayerScript and EnemyScript.

For example, I assume both PlayerScript and EnemyScript extend MonoBehaviour, so that’s one option for X.

public MonoBehaviour scriptOmega;

This will definitely work but whether or not this is a useful type here depends on what you want to do with scriptOmega.

1 Like

This just seems like a really bad idea. What are you trying to accomplish with this design?

1 Like

Yeah, actually now that I read your question again I see that you want X to change. This is not possible. But if you tell us what you want to do we might be able to offer some suggestions.

1 Like

Awesome thanks for the amazing quick response.

i guess the heart of my question was just how to pull data from many different scripts.

I am making an attack method, i want to pass it 2 game objects(attacker, and target). it will then check some stuff; weapon, armor and attributes.
but the problem is that the scripts i have made have different names for the player(PlayerScript) and the enemy(EnemyScript), and Other(OtherScript).
So the Attack() method is passed the 2 objects with different scripts and need to get data from each. i was hoping to make this method to soft the script by tag oth some other method and end up with a single variable.
as i am writing this it seems maybe the best idea is to just rename those scripts.

I’d create a new component script called, let’s say, Combat. Define your weapons, armour, resistences, attributes and what not in there. Add that new component to both your Player and Enemy and use that in your Attack function calculations.

That way, if you add a new type of combatant in the future, you don’t have to go back into this code and change anything.

2 Likes

Okay yeah that makes good sense. Thanks for the Help!

You could also use an interface. If both classes have a method named “Attack()”, you could make an interface:

public interface IAttacker
{
    void Attack();
}

then change the definitions of the two classes to implement that interface:

public class PlayerScript : MonoBehaviour, IAttacker
{ //...etc...
}

public class EnemyScript : MonoBehaviour, IAttacker
{ ///....stuff....
}

Then your type “X” in the first question could be IAttacker.

2 Likes

Oh Cool, i dont know what your doing with the interface. I haven’t done anything with that.
is it inheriting from both the Mono, and the IAttack?

so anything i add the :IAttack to will have all those methods contained in the IAttack?

Yes. But note you can inherit only one actual class (MonoBehaviour in this case), but many interfaces.

More like it will force you to add all the methods in IAttack to your class. You can’t put any actual code in the methods of an interface; it’s basically just saying “These are the methods that have to be in any class that inherits me”. The actual code for Attack() goes into each class that inherits it and they can all have different code, so the version of Attack() in PlayerScript could do player-specific stuff like update your stamina bar on the screen while the version of Attack() in EnemyScript could play an animation and make the enemy flash red or whatever.

1 Like

Though I guess I should add… if the code for the two different versions is almost all identical, then it would probably be better to have a single component that they both can share, like Grozzler suggested, so that you don’t just have two different copies of the same code in two different places.

1 Like

Awesome thanks. that is really helpful!