- Home /
Question by
davidflynn2 · May 22, 2013 at 01:32 PM ·
c#guibuttonmoveable
Making my GUI Button Movable in Play mode
I have been working on the below code and realy like it but is there any way I can make is so that the player can move the GUI button where ever they would like it to be. Here is how I coded my GUI button Up.
using UnityEngine;
using System.Collections;
public class Mounting : MonoBehaviour {
private bool isMounted = false; //This decides if we should be doing mounting or not.
private GameObject hoverBoards; //Place Holder For the Search.
private string button1Text; //Reqired for gui Text.
public GUISkin Location1Button; // GUI Skin For Text.
public Transform thisChar; //The Character you would like to put on mount.
public Transform spawn; //The spawn location needs to be an object that is parented to your character.
public GameObject prefabToSpawn; //This is the mount we are going to spawn.
void OnGUI()//Starts Our GUI Function
{
GUI.skin = Location1Button; //Turns our GUI Skin On.
if(isMounted == true)//Checks if we are mounted.
{
if (GUI.Button(new Rect(350, 100, 65, 65),button1Text))//Checks if the button has been pushed.
{
thisChar.GetComponent<ThirdPersonController>().enabled = true;//Turns on the Third Person Controller.
thisChar.transform.parent = null;//Makes the character not parented to anything.
hoverBoards = GameObject.Find("Hover Board(Clone)");//Find the Hover Board.
Destroy(hoverBoards);//Destroys the hover Board.
isMounted = false;//Tells us that we are not mounted.
}
}
if(isMounted == false)//Checks if we are mounted.
{
if (GUI.Button(new Rect(350, 100, 65, 65),button1Text))//Chekcs if the button has been pushed.
{
GameObject clone = Instantiate(prefabToSpawn,spawn.position, spawn.rotation)as GameObject;//Spawns the hover board.
thisChar.transform.parent = clone.transform;//Sets the character as a child of the mount.
thisChar.GetComponent<ThirdPersonController>().enabled = false;//Turns off the third person controller.
clone.GetComponent<HoverBoards>().enabled = true;//Turns on the hoverboard script.
isMounted = true;//tells us that we are mounted.
}
}
}
}
Comment
Your answer
Follow this Question
Related Questions
Multiple Cars not working 1 Answer
C# Why Won't My GUI Layout Button Appear? 1 Answer
I have created a button with photoshop and i need to use it in my menu 2 Answers
Key for GUI.Button 2 Answers