Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by adam_unity566 · Apr 26, 2020 at 01:01 PM · cameragameobjectbuttonscript.onclick

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); } }

Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

2 Replies

· Add your reply
  • Sort: 
avatar image
0

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

Comment
Add comment · Show 1 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image adam_unity566 · Apr 29, 2020 at 12:36 AM 0
Share

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.

avatar image
0

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.

Comment
Add comment · Show 2 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image adam_unity566 · Apr 29, 2020 at 12:55 PM 0
Share

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.

avatar image Smaika · Apr 29, 2020 at 05:22 PM 0
Share

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

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

269 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

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


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges