- Home /
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); } }
Answer by Cassos · Apr 28, 2020 at 09:57 PM
I really don't know what you want ^^
But I tried it anyway :P 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
Ok, this is not exactly what I wanted. I wanted the cameras to change in one way but on the click of a button. but I couldn't assign the gameobject with unity's input function.
Answer by Smaika · Apr 29, 2020 at 01:51 AM
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.
I have a script for that I just don't know how to link the game object that is holding this script in the OnClick field.
https://imgur.com/a/OjY49Js https://imgur.com/a/xpjTLNG Your functions must be set to public. I hope that is what you are looking for
Your answer
Follow this Question
Related Questions
void dont show up in on click 1 Answer
How to activate an object after another is deactivated 1 Answer
Is it possible to link a UI element to a gameobject? 1 Answer
How can i add a fuction to a button by code??? 0 Answers
GUI Button scripting 0 Answers