Author Topic: Enable GameObject on button click.  (Read 7456 times)

FWCorey

  • Newbie
  • *
  • Thank You
  • -Given: 0
  • -Receive: 0
  • Posts: 13
    • View Profile
Enable GameObject on button click.
« on: September 29, 2012, 10:17:11 PM »
I'd like a button to enable a GameObject and it's children when clicked.
Do I need to write a script for this and trigger it with the OnClick() event or am I missing something? It seems like a basic function that should already be in the system.

Also for some reason Screen.SetResolution(xxx,xxx,true) in the WebPlayer doesn't seem to work on my NGUI button. It's just ignoring the command. I've put Debug.Log messages in the same function so I know it's firing, but it's having no effect. Is there a setting in NGUI that I missed, that might be causing this behaviour?

ArenMook

  • Administrator
  • Hero Member
  • *****
  • Thank You
  • -Given: 337
  • -Receive: 1171
  • Posts: 22,128
  • Toronto, Canada
    • View Profile
Re: Enable GameObject on button click.
« Reply #1 on: September 29, 2012, 11:04:51 PM »
I use this code to toggle full screen mode in the web player:
  1. using UnityEngine;
  2.  
  3. [AddComponentMenu("Game/UI/Full Screen")]
  4. public class UIFullScreen : MonoBehaviour
  5. {
  6.         void OnClick()
  7.         {
  8.                 if (Screen.fullScreen)
  9.                 {
  10.                         Screen.SetResolution(1280, 720, false);
  11.                 }
  12.                 else
  13.                 {
  14.                         Screen.SetResolution(Screen.currentResolution.width, Screen.currentResolution.height, true);
  15.                 }
  16.         }
  17. }

To activate an object on click you can use ActiveAnimation.Play if you've got an animation on it, or write something quick like this:
  1. using UnityEngine;
  2.  
  3. public class ActivateOnClick : MonoBehaviour
  4. {
  5.         public GameObject target;
  6.         public bool state = true;
  7.  
  8.         void OnClick () { NGUITools.SetActive(target, state); }
  9. }

FWCorey

  • Newbie
  • *
  • Thank You
  • -Given: 0
  • -Receive: 0
  • Posts: 13
    • View Profile
Re: Enable GameObject on button click.
« Reply #2 on: September 30, 2012, 10:49:45 AM »
Thanks for the help mate. I'm still surprised a simple GameObject Enable/Disable wasn't included in the package though.

Nicki

  • Global Moderator
  • Hero Member
  • *****
  • Thank You
  • -Given: 33
  • -Receive: 141
  • Posts: 1,768
    • View Profile
Re: Enable GameObject on button click.
« Reply #3 on: September 30, 2012, 11:18:07 AM »
That's because it's so simple.
You just have a button that's active at all times and do something like this

  1. [SerializeField] GameObject myActivatableGameObject; //set reference in inspector
  2.  
  3. void OnClick()
  4. {
  5.     NGUITools.SetActive(myActivatableGameObject, myActivatableGameObject.active);
  6.  
  7. }
  8.  


FWCorey

  • Newbie
  • *
  • Thank You
  • -Given: 0
  • -Receive: 0
  • Posts: 13
    • View Profile
Re: Enable GameObject on button click.
« Reply #4 on: October 01, 2012, 12:38:12 PM »
Actually it's short, not simple. It's simple if you already know about the NGUITools script and it's functions, but my artist can't be expected to know that. I just use Visual Studio and compile dll's with monodevelop while he uses Unity for the scene editing etc...