Instantiate prefab doesn't assign variables

I try to enable and disable different canvases with a left mouse click on game objects with Raycasts. But I instantiete it from prefabs so it doesnt save the Main Camera and Canvas I want to disable and enable.

I know how to do it with GameObject:

cameraCheck = GameObject.FindWithTag( "MainCamera" );

but GameObject and Canvas/Camera dont have a FindWithTag? So I try FindObjectOfType which gives me all kinds of errors.

void Start()
    {
        canvasBuilding.enabled = false;
        cameraCheck = FindObjectOfType<Camera>.tag "MainCamera";
        canvasBuilding = FindObjectOfType<Canvas>.tag "Building_01";
    }
void Start()
    {
        canvasBuilding.enabled = false;
        cameraCheck = Camera.FindObjectOfType<Camera>().tag "MainCamera";
        canvasBuilding = Canvas.FindObjectOfType<Canvas>().tag "Building_01";
    }

It just all doesnt seem to work.

Thank you for your time :slight_smile:

First off, you can replace

cameraCheck = GameObject.FindWithTag( "MainCamera" );

with

cameraCheck = Camera.main;

And if you want to keep track of the objects you’re instantiating then just store them when you create them
eg.

Canvas canvasBuilding = Instantiate(CanvasPrefab);

Thanks! Camera.main; worked!

Only I would like to set my canvas similar, and call it by its tag. Ive tried

canvasBuilding = Canvas.FindObjectOfType<Canvas>().GetComponent<BuildingUnitsCanvas>();

I can’t know how to set it up in Start() to the right Canvas I want to enable and disable

Thank you :slight_smile: