How do I attach a GameObject when the script is attached to unity's on click () function (for buttons).

Sorry if I wasn’t clear in the question it is hard to describe. I declare a game object in my script. Now this script is only used when the player clicks a button. I used unity’s built-in on click() function so it doesn’t give me the usual box where I can select which camera I want my gameobject to be. Should I just do a manual OnButtonClick or something like that? Is there a solution to this? Here is the code `using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class StartButton : MonoBehaviour
{
public GameObject GameCamera1;
public GameObject ShopCamera1;
public GameObject MenuCamera1;

// Update is called once per frame
void Update()
{
GameCamera1.SetActive(true);
MenuCamera1.SetActive(false);
ShopCamera1.SetActive(false);
}
}`

I really don’t know what you want ^^

But I tried it anyway :stuck_out_tongue: Hop that’s what you wanted

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class StartButton : MonoBehaviour {
    public GameObject GameCamera1; 
    public GameObject ShopCamera1; 
    public GameObject MenuCamera1; 

    void Update() 
    {
        if (Input.GetKeyDown(KeyCode.Alpha1))
            SelectCamera(GameCamera1.GetComponent<Camera>());

        if (Input.GetKeyDown(KeyCode.Alpha2))
            SelectCamera(ShopCamera1.GetComponent<Camera>());

        if (Input.GetKeyDown(KeyCode.Alpha3))
            SelectCamera(MenuCamera1.GetComponent<Camera>());
    } 

    public void SelectCamera(Camera c)
    {
        if (c == null)
            return; // Stops the function if there is no camera

        // Disabling all cameras
        GameCamera1.SetActive(false);
        ShopCamera1.SetActive(false);
        MenuCamera1.SetActive(false);

        // Enabling selected camera
        c.gameObject.SetActive(true);
    }
}

You can select the gameCamera by pressing the “1” key, shopcamera by pressing “2” and the menuCamera by pressing “3”. It’s very simple.

if you should add other cameras add them to the disabling list in the SelectCamera() function.

Greetings,

Max

I’m not sure if this is what you want but try something like this,

using System.Collections; 
    using System.Collections.Generic; 
    using UnityEngine; 
    
    public class StartButton : MonoBehaviour { 
    
    public GameObject GameCamera1; 
    public GameObject ShopCamera1; 
    public GameObject MenuCamera1; 
    
    // Using update to call a function repeatedly is usually not a good idea
    //Try this instead
    
    void TurnGameCamera(bool value){
    GameCamera1.SetActive(value);
    MenuCamera1.SetActive(false); 
    ShopCamera1.SetActive(false);
    }
    
    void TurnMenuCamera(bool value){
    MenuCamera1.SetActive(value);
    GameCamera1.SetActive(false); 
    ShopCamera1.SetActive(false);
    }
    
    void TurnShopCamera(bool value){
    ShopCamera1.SetActive(value);
    GameCamera1.SetActive(false); 
    MenuCamera1.SetActive(false);
    }
}

You would then link the game object that is holding this script in the OnClick field and call the function you want.

I hope that helps.