Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice
  3. Join us on November 16th, 2023, between 1 pm and 9 pm CET for Ask the Experts Online on Discord and on Unity Discussions.
    Dismiss Notice

Varibles for Data types?

Discussion in 'Scripting' started by tomjoe, Jul 24, 2015.

  1. tomjoe

    tomjoe

    Joined:
    May 11, 2015
    Posts:
    44
    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:

    Code (CSharp):
    1. public X scriptOmega;
    2.         void Start() {
    3.             if (this.gameObject.tag == "Player") {
    4.                 scriptOmega = GetComponent<PlayerScript>();
    5.             }
    6.             if (this.gameObject.tag == "Enemy") {
    7.                 scriptOmega = GetComponent<EnemyScript>();
    8.             }
    9.    
    10.     }
    So the data type(X) will change to the corresponding script(PlayerScript or EnemyScript).

    is there method for this?
     
  2. apparition

    apparition

    Joined:
    Jan 11, 2012
    Posts:
    116
    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.

    Code (CSharp):
    1. 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.
     
    tomjoe likes this.
  3. GroZZleR

    GroZZleR

    Joined:
    Feb 1, 2015
    Posts:
    3,201
    This just seems like a really bad idea. What are you trying to accomplish with this design?
     
    tomjoe likes this.
  4. apparition

    apparition

    Joined:
    Jan 11, 2012
    Posts:
    116
    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.
     
    tomjoe likes this.
  5. tomjoe

    tomjoe

    Joined:
    May 11, 2015
    Posts:
    44
    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.
     
  6. GroZZleR

    GroZZleR

    Joined:
    Feb 1, 2015
    Posts:
    3,201
    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.
     
    apparition and tomjoe like this.
  7. tomjoe

    tomjoe

    Joined:
    May 11, 2015
    Posts:
    44
    Okay yeah that makes good sense. Thanks for the Help!
     
  8. makeshiftwings

    makeshiftwings

    Joined:
    May 28, 2011
    Posts:
    3,350
    You could also use an interface. If both classes have a method named "Attack()", you could make an interface:

    Code (csharp):
    1. public interface IAttacker
    2. {
    3.     void Attack();
    4. }
    then change the definitions of the two classes to implement that interface:

    Code (csharp):
    1. public class PlayerScript : MonoBehaviour, IAttacker
    2. { //...etc...
    3. }
    4.  
    5. public class EnemyScript : MonoBehaviour, IAttacker
    6. { ///....stuff....
    7. }
    Then your type "X" in the first question could be IAttacker.
     
    apparition and GroZZleR like this.
  9. tomjoe

    tomjoe

    Joined:
    May 11, 2015
    Posts:
    44
    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?
     
  10. makeshiftwings

    makeshiftwings

    Joined:
    May 28, 2011
    Posts:
    3,350
    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.
     
    tomjoe likes this.
  11. makeshiftwings

    makeshiftwings

    Joined:
    May 28, 2011
    Posts:
    3,350
    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.
     
    tomjoe likes this.
  12. tomjoe

    tomjoe

    Joined:
    May 11, 2015
    Posts:
    44
    Awesome thanks. that is really helpful!