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:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
public class ScrollAddRemoveInt: MonoBehaviour
{
#region Variables
[Header("Set up needed")]
public GameObject Mother;
public GameObject Oc;
public int Value;
#endregion
public void Start()
{
#region Get all the values
Value = Oc.GetComponent<OcTree>().Find(Mother.Name);
#endregion
}
}
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.
@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 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.
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.
public class ScrollAddRemoveInt: MonoBehaviour
{
#region Variables
[Header("Set up needed")]
public OcTree Mother;
#endregion
public void Start()
{
string name = Mother.Name;
}
}
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#.
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.