so one of the things that’s really big with gameobject.find is where you have it. Usually you have it at the start method, oncollision or ontriggerenter.
If you don’t have a handler (variable declared at the beginning ie private Player player) for the game object and then a declaration to find it somewhere such as
private void start()
{
//needs to be capitalized
player = GameObject.Find(“name of thing you’re finding”) and then the GetComponent<> of that game object you specifically are wanting, it won’t work.
}
Personally I like to use static
public static Player ninja;
public so any script can access any compenents within that player script
static so that it doesn’t change throughout movement of of anything else in the app
Player which happens to be the name of my script so what ever script you’re in will be that 3rd word
ninja just the name i gave it within that sript could have been steve
so now you need 2 more things
in the script you declared static you need the following Method at the beginning, this will make sure that you only ever have one of it in existence at a time (works even if i do this with enemies and spawn in 5000 of them, it just makes sure that there is only one instance of each desired instantiation
private void Awake()
{
if (ninja == null)
{
ninja = this;
}
else if (ninja != this)
{
Destroy(gameObject);
}
}
then in whatever script you were trying to find the game object you would simply write this line and adjust it accordingly
Player.ninja.lives = 4;
or Player.ninja.Movement();
so the first word is the name of the Script
the second is what you decided to name the script
the last part is the component of the script you’re trying to access.
it can be longer than 3 pieces but the first 2 are always
CLASS.NAME.everything else.
if you’re deep in your game, from experience, it can be really cumbersome to translate everything into Singletons (which is what i am recommending)
but for GameObject.Find() you’ll need the following lines of GetComponent<>(“component”).whatever the rest of your compoenent is