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. Dismiss Notice

Resolved How To Use Either/Or?

Discussion in 'Scripting' started by AdamEarlston, Jul 6, 2021.

  1. AdamEarlston

    AdamEarlston

    Joined:
    Jun 7, 2019
    Posts:
    71
    Hey, sorry for the basic question, but how can I code this script to check if the
    Code (CSharp):
    1. player.GetComponent<MeshCollider>()
    exists on the player and if true, continues to do .enabled = false;
    And if it isn't true/existing on the player, then do this
    Code (CSharp):
    1. player.GetComponent<BoxCollider>().enabled = false;
    I've just started working on my game project again, and after adding in multiple characters with each 1 either using a Mesh Collider or Box Collider, it causes my script to not work due to what's stated above, i could probably figure it out after hours of googling/testing (can't think of the correct words to google for finding guides for it atm), so any help is greatly appreciated!

    Full Script:

    Code (CSharp):
    1. using UnityEngine;
    2.  
    3. public class EndTrigger : MonoBehaviour {
    4.  
    5.     public GameManager gameManager;
    6.     public GameObject audioman;
    7.     public GameObject Object;
    8.     public GameObject player;
    9.  
    10.  
    11.  
    12.     void Start()
    13.     {
    14.         player.GetComponent<MeshCollider>();
    15.         player.GetComponent<BoxCollider>();
    16.         player.GetComponent<AudioSource>();
    17.         GameObject.Find("AudioManager");
    18.         Object.SetActive(false);
    19.     }
    20.  
    21.     void OnTriggerEnter (Collider other)
    22.     {
    23.         if (other.gameObject.tag == "Player")
    24.         {
    25.             PlayerPrefsManager.coins += 50;
    26.             PlayerPrefsManager.UpdateCoins();
    27.             gameManager.WinLevel();
    28.             gameManager.CompleteLevel();
    29.             player.GetComponent<MeshCollider>().enabled = false;
    30.             player.GetComponent<BoxCollider>().enabled = false;
    31.             player.GetComponent<AudioSource>().enabled = false;
    32.             Destroy(audioman);
    33.             Object.SetActive(true);
    34.         }
    35.     }
    36. }
    37.  
     
  2. RadRedPanda

    RadRedPanda

    Joined:
    May 9, 2018
    Posts:
    1,593
    Pretty sure you can just do
    GetComponent<Collider>()
    and that will automatically get the collider you want, since you can only have one on an object.
     
    Bunny83 and AdamEarlston like this.
  3. AdamEarlston

    AdamEarlston

    Joined:
    Jun 7, 2019
    Posts:
    71
    Err, yup lol, I can confirm that it works perfectly, thank you >.<
     
  4. RadRedPanda

    RadRedPanda

    Joined:
    May 9, 2018
    Posts:
    1,593
    Just some words of advice, in your Start method you aren't actually doing anything with the first 4 lines of code, you want to set those to a reference so you can use them later.

    Code (CSharp):
    1. using UnityEngine;
    2. public class EndTrigger : MonoBehaviour {
    3.     public GameManager gameManager;
    4.     public GameObject audioman;
    5.     public GameObject Object;
    6.     public GameObject player;
    7.  
    8.     private AudioSource audioSource;
    9.     private Collider collider;
    10.     void Start()
    11.     {
    12.         collider = player.GetComponent<Collider>();
    13.         audioSource = player.GetComponent<AudioSource>();
    14.         audioman = GameObject.Find("AudioManager");
    15.         Object.SetActive(false);
    16.     }
    17.     void OnTriggerEnter (Collider other)
    18.     {
    19.         if (other.gameObject.tag == "Player")
    20.         {
    21.             PlayerPrefsManager.coins += 50;
    22.             PlayerPrefsManager.UpdateCoins();
    23.             gameManager.WinLevel();
    24.             gameManager.CompleteLevel();
    25.             collider.enabled = false;
    26.             audioSource.enabled = false;
    27.             Destroy(audioman);
    28.             Object.SetActive(true);
    29.         }
    30.     }
    31. }
    There's some other things which could use improvements, but if you see what I did with collider, audioSource, and audioman you should understand what I mean, if not just shoot another question my way.
     
    Bunny83 likes this.
  5. AdamEarlston

    AdamEarlston

    Joined:
    Jun 7, 2019
    Posts:
    71
    Took a while to reply due to making a back up copy just in case of any issues, but yeah i don't understand what your changes did (no visual difference i think?), i'm a total noob with everything dev wise, didn't go to school or anything for coding, i just learned a few things from youtube guides and Brackeys.

    Slowly remembering a few things though, if i'm right, you basically made it easier/possible to refference the stuff in other scripts? (If i made them public instead of private)
    In my eyes, both the old and new achieve the same thing exactly though?

    Edit: Or does the "GetComponent" in void Start cause pointless performance loss at the start?
     
  6. RadRedPanda

    RadRedPanda

    Joined:
    May 9, 2018
    Posts:
    1,593
    So I'll cover some stuff in some detail, sorry if it sounds condescending, just trying to cover all the bases.

    A method is anything you can call with parenthesis at the end, like
    GetComponent()
    and
    updateCoins()
    , as well as
    Destroy(object)
    , which takes some values as input. You can imagine all of these as magical boxes, which when you press the button on them, they do something.

    GetComponent()
    is slightly different though, as it returns a value, and doesn't change anything else. You can think of this as if when you press the button, it spits out a component. You can then take that component and put it somewhere else.

    Let's pretend that what's running our code is a little robot in a warehouse, and they perform each line of code one at a time. Each line, the robot goes through and presses the buttons on the
    GetComponent()
    boxes, and they each spit out a component. However, what you're doing is having the robot press the button, and move on to the next box. The component that it spits out just falls to the floor and breaks. Sometimes that's fine if you don't care about what the box spits out, but in our case we do.

    These two lines have some parallels, and I'll use them to explain what you did wrong.

    Code (CSharp):
    1. collider = player.GetComponent<Collider>();
    2. PlayerPrefsManager.coins = 50;
    So in this code for our coins, we're setting the amount of coins to 50. In the collider line, we're setting the collider value to the Component. If we had what you had:
    Code (CSharp):
    1. player.GetComponent<Collider>();
    would basically be like saying:
    Code (CSharp):
    1. 50;
    Just a value sitting there doing nothing.

    A variable is kind of like a storage area, like a cabinet, or a labeled box, and in your code, they're the things at the top like
    public GameObject player
    . It's basically some place the robot can put an object down without it breaking.
    Code (CSharp):
    1.     public GameManager gameManager;
    2.     public GameObject audioman;
    3.     public GameObject Object;
    4.     public GameObject player;
    5.  
    6.     private AudioSource audioSource;
    7.     private Collider collider;
    What we want to do is take that Component that got spit out, and save it for later, and to do that we can make a variable to store it. In my code, I created a variable by saying
    private Collider collider
    , which makes a storage area where you can only put Colliders. To put it in that spot, we call the line I wrote to put it in there.
    Code (CSharp):
    1. collider = player.GetComponent<Collider>();
    Now, whenever we want to do something to our component, we don't have to have the robot hit the button to get the Component anymore, we have one lying around. This is where you can tell your robot to check the spot named collider, and just use that.
    Code (CSharp):
    1. collider.enabled = false;
    Of course, this example doesn't cover all the bases, but it should give you an idea of how references to objects work.
     
    Pykrete and AdamEarlston like this.
  7. AdamEarlston

    AdamEarlston

    Joined:
    Jun 7, 2019
    Posts:
    71
    Thanks :p and yeah i understand now, i don't really know much about basics like this, so thanks for taking the time to teach me :D
     
    RadRedPanda likes this.
  8. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    36,762
    You should take a peek-see at these great Unity3D scripting tutorials here... your brain will expand and open in amazing ways you never thought possible. In fact you might even need to purchase a larger skull soon.

    Imphenzia / imphenzia - super-basic Unity tutorial:



    Jason Weimann:



    Brackeys super-basic Unity Tutorial series:



    Sebastian Lague Intro to Game Development with Unity and C#:



    Imphenzia: How Did I Learn To Make Games:



    Basics of Unity GameObjects and Components:

    https://youtu.be/9Nf2_ds5y8c
     
    AdamEarlston likes this.