Passing references between gameobjects

So i have a Prefab that will instantiate in the scene, but certain script inside this prefab needs some references. I figured that i could have a gameobject that holds all these references, but i dont know how to pass this from a gameobject to other

Heyho NotamGames,
when Instantiating Prefabs, you can get a reference to the currently instantiated Prefab and use it’s functions to give a reference.

Example:
We have 3 scripts: Master, Minion and Knowledge.
We have 1 prefab: ExamplePrefab, with Minion script attached.
We have 2 objects in scene:

  • Object GameMaster with the Knowledge Script attached.
  • Object Master with the Master script attached.
    For the Master script of Master: The Prefab is referenced in the inspector by dragging the prefab from the project folder into the slot and the knowledge is referenced in the inspector by dragging the GameManager into the Knowledge slot.

The Master has a reference to both the ExamplePrefab and the Knowledge.

public class Master : MonoBehaviour
{
    // References to

    // The Knowledge
    [SerializeField]
    private Knowledge knowledge;

    // The ExamplePrefab
    [SerializeField]
    private GameObject ExamplePrefab;



    private void MakePrefab()
    {
        GameObject go = Instantiate(
            ExamplePrefab,
            Vector3.zero,
            Quaternion.identity,
            this.transform
        );

        go.GetComponent<Minion>().UpdateKnowledge(this.knowledge); // "this." is not neccessary, but it helps preventing bugs
    }
}

It also has the function MakePrefab(), which will Instantiate a prefab at 0,0,0 with default rotation and this as a parent.

It then accesses the minions function UpdateKnowledge() and sends over this reference to the knowledge.

public class Minion : MonoBehaviour
{
    // References

    // To the knowledge
    private Knowledge knowledge;

    // Overwrite knowledge
    public void UpdateKnowledge(Knowledge knowledge)
    {
        this.knowledge = knowledge;
    }
}

After we call MakePrefab(), we now have successfully instantiated a prefab and changed one of it’s paramaters!

I tested it, so it works not only theoretically.

I don’t understand. Do you want to get the references on the parent gameobject? or do you need to get them from inside the scene from other objects? If it’s the first one, just by getting all the references on the Start() method should get you the variables that you need, since start initiates at the moment the object loads into the scene rather than the start of the game.

A lot of people face this same problem since prefabs can’t register references like other Game Objects (Drag and Drop in the inspector) but There are different ways to solve this problem. I’ll just list some

  1. Use
GameObject newRef = GameObject.Find("GameObjectName");

{Slows down performance}

  1. You can also use
GameObject newRefByTag = GameObject.FindWithTag("GameObjectTag");

{Bad in situations where not only the object has that tag}

  1. Let the object to be referenced also be a prefab, then you can reference a prefab from a prefab and instantiate it too. Or let it be a child of the first prefab

Hope this helps✌️!

Another option is to have the script which instantiates the prefab set the references in the new object immediately after it is created. This only works if the script which spawns the prefab is not also a prefab.