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

Am I doing it it?

Discussion in 'Scripting' started by cyboerg, Jan 28, 2015.

  1. cyboerg

    cyboerg

    Joined:
    Jan 28, 2015
    Posts:
    9
    Hey guys!
    Im fairly new to coding so I would just like to know if this is the right way of doing it.

    I have a character named Engineer.
    a medikit with a tag "Large_Health
    a mine with a tag "Mine"
    AND a Engineer_Health script applied to the Character

    Then the player enters the trigger of one of them the mine or health "effect"r uns within the Engineer_Health script.
    Is this the right way or should the mine and health have their own scripts attached that handles what is dealt to the player?

    Code (CSharp):
    1. public class Engineer_Health : MonoBehaviour
    2. {
    3.     public int startingHealth = 100;
    4.     public int currentHealth;
    5.  
    6.     public int mineDamage;
    7.  
    8.     void Awake()
    9.     {
    10.         currentHealth = startingHealth;
    11.     }
    12.  
    13.     void OnTriggerEnter(Collider other)
    14.     {
    15.         if(other.gameObject.tag == "Health_Large")
    16.         {
    17.             currentHealth = startingHealth;
    18.         }
    19.  
    20.         if(other.gameObject.tag == "Mine")
    21.         {
    22.             mineDamage = Random.Range(50,100);
    23.             currentHealth = currentHealth - mineDamage;
    24.         }
    25.  
    26.     }
    27. }
     
  2. DarkEcho

    DarkEcho

    Joined:
    Jul 7, 2014
    Posts:
    231
    You can do it either your way (Your not wrong) or you can have it so the Mine & Health Kit gameobjects have their own scripts using a Collider so the player can access the Mine & Health Kits assigned scripts.

    You are not wrong, however I would go for the second option.

    Dont worry about HOW you code it, there is more than one way to achieve something with code. Therefore what ever-way you chose,you are not wrong, go with your gut.

    The Unity community is not big on checking if your taking a good route towards an objective, so just go with what ever solution you want, dont worry about HOW it is done, as long as its done.

    Do you see where im coming from?
     
  3. cyboerg

    cyboerg

    Joined:
    Jan 28, 2015
    Posts:
    9
    Yeah, more than 1 way to skin a cat.
    Although I don't get the 2nd option your describing?

    I do realize my method wont be best if id want to reuse a script.
     
  4. DarkEcho

    DarkEcho

    Joined:
    Jul 7, 2014
    Posts:
    231
    Yes it is a good idea to reuse the same script over and over. However this does not mean using just ONE script on ONE gameobject.

    It just means using the same SINGLE script on over 100 gameobjects. For example with your Mine, if you had over 100 Mines using the same script, its still using the same script once. So you got nothing to worry about.

    My second solution is what I mentioned above. All Mines have a script.
     
  5. Kiwasi

    Kiwasi

    Joined:
    Dec 5, 2013
    Posts:
    16,860
    This will get difficult fast. Checking literal strings is prone to typos and errors. Every time you add a new item you have to modify your Engineer_Health script. You could end up doing hundreds of string comparisons every time you collide. Bad, bad, bad.

    A better way is for the Engineer_Health script to expose a few public methods that the objects can interact with (preferably in an interface). Then each object simply calls the appropriate method in its OnTriggerXXX. Totally modular code, the engineer is now decoupled from the items. He does not care what items exist, and the items do not care that the engineer exists, except for a very small interface.
     
  6. cyboerg

    cyboerg

    Joined:
    Jan 28, 2015
    Posts:
    9
    I totally get what your saying and it makes sense to me now.

    So each object say mine and health each have their own script assigned that says what it has to do to the player health ontrigger.