Reading a script on a cloned prefab from another script

Hi guys

I have a script on a prefab called TopHorizontalDestination
in here is an int called destinationPadNumber
The prefab is then instantiated via another script called PassengerManager

on my landing pad i have a script that im using to detect when the player has landed and i will also use this to compare if the players destination and the landing pad match.ie the passenger is at the correct pad.

on my landing pad script im trying to get a reference to the destinationPadNumber on the passenger clone:

public TopHorizontalDestination thDestination;
private void Start()
    {
        thDestination = GameObject.Find("HorizontalTop(Clone)").GetComponent<TopHorizontalDestination>();
    }

But this just isnt working, i have tried many ways to get to the clones int on its script but nothing seems to work.
please help.

Remember the first rule of GameObject.Find():

Do not use GameObject.Find();

More information: https://starmanta.gitbooks.io/unitytipsredux/content/first-question.html

Also, you gotta find WHAT isn’t working.

Got you! will apply an alternative.
Is my reference correct though? can you use “HorizontalTop(Clone)”?
How do you actually reference an instantiated object in the hierachy?

It is clear you did not scroll down to the 50% mark on the manta link I enclosed above. Hie thee to it… it’s all there.

ok cool thanks Kurt!!!

Hey i read the doc and although it explains what should be used instead of Gameobject.Find it doesnt say how to reference clones made from prefabs. But if it does i cant see where. Can you just point me to a few keywords i should read again. please

It does!!!

Um… how about either the word “instantiate” or “prefab?”

7764300--978234--Screen Shot 2021-12-27 at 6.21.51 AM.png

You may not need an array of them, so just make one and store it.

I didnt realise it was to do with “Storage Upon Instantiation” sorry, was looking for referencing or something similar.

This is how im instantiating my clones from a prefab. But i cant get to any variables of my instantiated objects from a different script.
I cant use pass1. anything and i cant make a reference from a different script to the clone object i have instantiated.
anything i try just fails with a reference error.

public GameObject topHorizontal;

Void Start {
GameObject pass1 = Instantiate(topHorizontal, new Vector2(horizontal1TopX, horizontal1TopY), Quaternion.Euler(new Vector3(0, 0, 0)));
}

Sorry if you finding it frustrating to help me.

How can i reference pass1 from another script?

It is axiomatic that if you need access to something later or elsewhere in the program, you need to make a reference to it somewhere that both places can get access to it.

This is the fundamental reason you would make a GameManager. It manages all the different parts of the game, helping them find each other, controlling their lifetimes, etc.

https://www.youtube.com/watch?v=4I0vonyqMi8

ULTRA-simple static solution to a GameManager:

https://discussions.unity.com/t/855136/6

https://gist.github.com/kurtdekker/50faa0d78cd978375b2fe465d55b282b

OR for a more-complex “lives as a MonoBehaviour” solution…

Simple Singleton (UnitySingleton):

Some super-simple Singleton examples to take and modify:

Simple Unity3D Singleton (no predefined data):

https://gist.github.com/kurtdekker/775bb97614047072f7004d6fb9ccce30

Unity3D Singleton with a Prefab (or a ScriptableObject) used for predefined data:

https://gist.github.com/kurtdekker/2f07be6f6a844cf82110fc42a774a625

These are pure-code solutions, do not put anything into any scene, just access it via .Instance!

If it is a GameManager, when the game is over, make a function in that singleton that Destroys itself so the next time you access it you get a fresh one, something like:

public void DestroyThyself()
{
   Destroy(gameObject);
   Instance = null;    // because destroy doesn't happen until end of frame
}