- Home /
How to get first script from gameobject while having propery : Unity
hello, I would like to get the first script in an gameobject, and still be able to set and get proper's of it. I'm coding in c# in unity I have tried "GetComponents(typeof(MonoBehaviour))" , "MonoBehaviour[] scripts = gameobject.GetComponents()" and other methods but all of that methods does not let you set and get
I dont know if it is even possible, and if it is not a i work around would be nice. Thanks
(If you need more info about this please comment and ill try to answer)
Answer by jackmw94 · Feb 05, 2021 at 03:59 PM
When you write gameObject, this refers to the gameObject that that script is attached to. If you want to refer to a different game object then you'll need a reference to it.
Getting all monobehaviours could return you all sorts of stuff, this will be easier if you know the type of script you want. For example, if you want to get the Image attached to a gameobject you can write Image image = gameObject.GetComponent<Image>();
.
When you say "first" script, it's generally bad practice to rely on the order of scripts on a gameobject as these can change pretty easily. If you have multiple scripts of the same type on a gameobject then you can differentiate them either by setting different settings on them or consider adding them to different gameobjects.
When you say get and set properties of it, this is absolutely possible if you have a reference to it. If the script is not written by you then you might be limited by what properties it allows you to change but generally anything hidden is done so for good reason!.
If it is your script then you can add functions to allow other scripts to get and set its properties:
public class MyScript : MonoBehaviour
{
[SerializeField] private int _myInt = 5;
public void GetInt()
{
return _myInt;
}
public void SetInt(int newValue)
{
_myInt = newValue;
}
}
This is a little example of a script that provides a getter and a setter for its variable called _myInt. If you had a different script that was attached to the SAME gameobject and wanted to get this script and change _myInt you could call:
public void ChangeTheIntegerOnMyScript()
{
// Get the script attached to the same gameobject as this script
MyScript myScript = gameObject.GetComponent<MyScript>();
// Set the int via the setter we defined
myScript.SetInt(10);
// Then print out our new value, just to show off
Debug.Log("Set the int on my script to " + myScript.GetInt());
}
If your script is attached to a different game object then you might need a reference to it in the script that you're calling it from. This is where you can use the [SerializeField] attribute to make variables appear in the inspector, just like we did with _myInt. Note that the following hints are only useful if the gameobjects are in the scene before we hit play - if you're creating new objects and want to get the components off them then stick to all of the above!
If you want a reference to a different gameObject then at the top of your script add in [SerializeField] private GameObject _someOtherGameObject;
and then you'll see this field appear in the inspector. You can drag other gameobject in here so that that script will have a reference to them. Once you've done that then you can call _someOtherGameObject.GetComponent...
to get scripts off that game object.
If you only need a certain script from that gameobject then instead of keeping a reference to the gameobject so that later we can call getcomponent, we can just write [SerializeField] MyScript _myScript;
to have this reference point directly to the script we're trying to get.
Sorry for the essay and I hope this helps. If you need more of a hand then comment with more information about your particular case :)