Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 14 Next capture
2021 2022 2023
2 captures
12 Jun 22 - 14 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 dagnabit_unity · May 07, 2018 at 03:26 AM · buttoneventsystembutton trigger eventsselectable

Add Button Script to 3D gameObject

Hi all,

I'm trying to add the Unity Button Script to a 3d gameObject. When I tried this by adding it onto a cube, I can't get the desired behaviours. How can I get the 3d object to work like a button? In particular the highlighting colors, the click events. I was speculating that perhaps it uses the wrong raycaster, and if the raycaster specificaiton is changed some how it would work? Does anyone have a good enough understanding of the source code to explain if this is possible, and if so what I would need to do to achieve this? Thanks.

Rik

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

1 Reply

· Add your reply
  • Sort: 
avatar image
0

Answer by Pinkuboxu · May 07, 2018 at 03:31 AM

Look into https://docs.unity3d.com/ScriptReference/MonoBehaviour.OnMouseDown.html then have it Invoke() a public or SerializedField UnityEvent that you can setup in the inspector, if you want to have something similar to the OnClick event that a UI button has. https://docs.unity3d.com/Manual/UnityEvents.html to understand UnityEvents. I can elaborate if needed.

I'm going to elaborate anyway and not be lazy. Your Cube code can look like this, but this is just an example so it's simple enough:

 using System.Collections;
 using System.Collections.Generic;
 using UnityEngine;
 using UnityEngine.Events; // this namespace makes the magic, tho
 
 public class OnClickCube : MonoBehaviour {
 
     [SerializeField] UnityEvent anEvent; // puts an easy to setup event in the inspector and anEvent references it so you can .Invoke() it
 
     // This captures a click as long as you have a collider, even if it's set to just be a trigger, and nothing blocking it.
     // However, it will still capture even if a Gui button is on top of it, so make sure you aren't checking this while also checking Gui
     // Only other colliders block and it's not as manageable as Canvas Groups.
 
     private void OnMouseDown()
     {
         print("You clicked the cube!");
         anEvent.Invoke(); // Triggers the events you have setup in the inspector
     }
 
     // This is the first method the event is setup to do, the second audio part needed no script to just do a one shot effect, thanks to the event system.
     // You just set up the Event in the inspector for easy peasy, but the UnityEvent could also be coded the same way if needed.
     public void EventClick() // methods have to be public void to be used by UnityEvents, they can't really return anything either, as far as I know... At least I don't know how an event will capture the return...
     {
         print("Which also triggered this method as a UnityEvent!");
     }
 }

Put it on a cube and set up the cube in the inspector thusly so: make it so!

And that should make an object be more like a UI Button. But you can't actually reuse the same UI button script, but this is technically the same idea. (You gotta come up with your own Click sound asset though.) As far as highlighting, you'll want to change the material values at the same time... I'll have to help with that later though if needed. I have to pass out...


onclickobject.jpg (64.0 kB)
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 Naxionman1978 · Jul 03, 2020 at 11:56 AM 0
Share

Can you please explain why this script is not resuable? I managed to make one of my 3d object work ok , and so did the others

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

102 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

Related Questions

Selecting a selectable using script doesn't seem to work anymore? 1 Answer

Unusual trouble with OnPointerClick 1 Answer

How to detect which UI Button was triggered via Script (and not actually clicked)? 0 Answers

Click/touch object to enable/disable buttons inside Canvas 1 Answer

RPG Maker style menu with UI buttons that are selected by controller 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