Search Unity

  1. Unity 2020.1 has been released.
    Dismiss Notice
  2. We are looking for feedback on the experimental Unity Safe Mode which is aiming to help you resolve compilation errors faster during project startup.
    Dismiss Notice
  3. Good news ✨ We have more Unite Now videos available for you to watch on-demand! Come check them out and ask our experts any questions!
    Dismiss Notice

GetComponent<stringNameToAddInEditor> ?

Discussion in 'Scripting' started by brancake, Jul 15, 2019.

  1. brancake


    Jan 10, 2018
    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    5. public class TriggerBool : MonoBehaviour {
    6.     public string gameObjectToReference;
    7.     public string scriptToReference;
    8.     public string boolToReference;
    9.     public string setBoolTo;
    11.     // Use this for initialization
    12.     void Start () {
    14.     }
    16.     // Update is called once per frame
    17.     void Update () {
    19.     }
    20.     private void OnTriggerEnter2D(Collider2D collision)
    21.     {
    22.         if( == "Player")
    23.         {
    24.             GameObject.Find(gameObjectToReference).GetComponent<scriptToReference>.boolToReference = setBoolTo;
    26.         }
    27.     }
    28. }
    This is the code I've got, but it cannot get the component of scriptToReference because it doesn't exist yet. I wanted to use this code as a multipurpose script to be used in many places, where the character enters a trigger collider and it sets a bool of a given script. I would like to use this in many places in the game I'm making, instead of making a lot of custom one line scripts.

    Sorry for any formatting issues, first time posting on this forum.
    Last edited: Jul 15, 2019
  2. silvematt


    Jul 15, 2016
    I think all you can do is this. (last example)

    Instead of doing
    as HingeJoint
    you can try
    as MonoBehaviour

    Let me know if it works.
    Last edited: Jul 15, 2019
  3. Antistone


    Feb 22, 2014
    As silvematt says, it's possible to call GetComponent(string nameOfType) instead of GetComponent<type>(), though it's not as efficient and you won't be protected from typos.

    But it looks like you're also string to use a string to specify the field name of a variable you want to set. There's no equivalent for that. There might possibly be something you can do involving reflection, but this is really the wrong approach.

    The best way to do this is probably to use events. If you've ever used a Unity Button, you know how you can use the inspector to tell it to call a specific function on a specific component when the button is clicked? You can write your own class to do the same thing, something like this (untested):
    Code (CSharp):
    1. using UnityEngine;
    2. using UnityEngine.Events;
    4. public class ReactToPlayerContact : MonoBehaviour {
    5.     public UnityEvent onPlayerContact;
    7.     private void OnTriggerEnter2D(Collider2D collision)
    8.     {
    9.          if( == "Player")
    10.          {
    11.             if (onPlayerContact != null) onPlayerContact();
    12.          }
    13.     }
    14. }

    Another way to do something like this in C# is with polymorphism. Define an interface or base class that contains the bool you want to modify (or a function that accepts a bool argument), and then make all the classes you want to reference from this script implement that interface or inherit from that base class. (For convenience, you probably want to use a base class that derives from MonoBehaviour, so that you get all the Unity editor built-in recognition of MonoBehaviours.)

    Then ideally you skip the "find" and give this class a direct reference to the component you are interested in by dragging it into a public field in the Unity inspector. But if you really need to find an object by name for some reason you can keep doing that part.
    silvematt likes this.