Search Unity

Resolved Find Variable of script by object name

Discussion in 'Scripting' started by Karma_XX, Mar 12, 2023.

  1. Karma_XX

    Karma_XX

    Joined:
    May 12, 2022
    Posts:
    10
    Hello,
    I'm trying to find the variable under the script in the gameobject, by knowing that the GameObject name is the same as one of those variables. Line 22.

    Script:

    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4. using UnityEngine.UI;
    5.  
    6. public class ScrollAddRemoveInt: MonoBehaviour
    7. {
    8.     #region Variables
    9.  
    10.     [Header("Set up needed")]
    11.     public GameObject Mother;
    12.     public GameObject Oc;
    13.  
    14.     public int Value;
    15.  
    16.     #endregion
    17.  
    18.     public void Start()
    19.     {
    20.         #region Get all the values
    21.  
    22.         Value = Oc.GetComponent<OcTree>().Find(Mother.Name);
    23.  
    24.         #endregion
    25.     }
    26. }
    27.  
    My lack of English knowledge is not helping me explain this problem.
    I'm sorry, i'll try to explain a bit more under here:

    Let's say that the Mother name is "Color".
    and the OcTree variables are: Name, Date, Color, Age.
    The value needs to be the int of the Mother name variable.
    Why I need to find it? Cause i will use this script under different Gameobject that needs different things.

    Thanks again
     
  2. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    38,726
    From your description above I'm not really sure what you are looking for.

    Are you perhaps looking for the
    Dictionary<TKey, TValue>
    Type?
     
  3. Kuro19980915

    Kuro19980915

    Joined:
    Jun 2, 2022
    Posts:
    2
    From the code it looks you are using public version of the Start function. If you are trying unity's start function it should be private.
     
  4. spiney199

    spiney199

    Joined:
    Feb 11, 2021
    Posts:
    7,919
    The accessibility modifier doesn't make a difference with Unity's magic methods.

    @OP, I don't understand what you're asking either. Do you just need a collection of your objects? A list, dictionary or similar?
     
    Karma_XX and Kuro19980915 like this.
  5. Karma_XX

    Karma_XX

    Joined:
    May 12, 2022
    Posts:
    10
    @spiney199 @Kuro19980915 @Kurt-Dekker
    ops idk how to explain...
    Well you know that if you put a script under a gameobject, there appears all the variables?
    Well i'm trying to get the value of one of those variables from another script, by knowing only that the name of the variable i need to find is the same name as Mother.
    Imma add a picture.

    As you can see from the picture, Mother can be the gameobject named(Height, Body Size, Head Size) in the hierarchy.
    The script knows who's holding the script at the moment. If "Height" is holding it, then it must find the variable "Height" on the bottom right.
    I know the script, as infact i take it with
    Code (CSharp):
    1. Value = Oc.GetComponent<OcTree>()
    But idk how to get the variable "height" in this case, by just knowing that the mother name is the variable name.
     

    Attached Files:

  6. spiney199

    spiney199

    Joined:
    Feb 11, 2021
    Posts:
    7,919
    You don't find the name of individual fields by their name.

    In Unity, you get a reference to a game object, or a component on a game object, then you can access the values of said object.

    Rather than reference 'Mother' as a game object, change your field to your OcTree type (make sure to assign it in the inspector again), and then you're good to go.

    Code (CSharp):
    1. public class ScrollAddRemoveInt: MonoBehaviour
    2. {
    3.     #region Variables
    4.     [Header("Set up needed")]
    5.     public OcTree Mother;
    6.     #endregion
    7.     public void Start()
    8.     {
    9.         string name = Mother.Name;
    10.     }
    11. }
     
    Karma_XX and Bunny83 like this.
  7. Karma_XX

    Karma_XX

    Joined:
    May 12, 2022
    Posts:
    10
    Thanks but idk how to use it...
    Idk how to get the value of the variable with the "Mother" name.
     
  8. spiney199

    spiney199

    Joined:
    Feb 11, 2021
    Posts:
    7,919
    Names of fields are just names. It's what type of data they represent which is more important.

    Just assign the 'Mother' field via the inspector (you can drag the game object with the component into the field). Then you have access to all its public fields/properties/methods/etc.

    If you don't understand me, you will need to take the time to get to grips with the fundamentals of C#.
     
    Karma_XX and Bunny83 like this.
  9. Bunny83

    Bunny83

    Joined:
    Oct 18, 2010
    Posts:
    3,998
    Like Spiney said here:
    You can directly reference a component on another gameobject in that "Mother" variable. Since you now have a direct reference to that component, you can directly access any field on that component like Spiney has shown.
     
    Karma_XX and spiney199 like this.
  10. Karma_XX

    Karma_XX

    Joined:
    May 12, 2022
    Posts:
    10