- Home /
Please help with player selection screen
Hey fellas I have a picture that shows whats going on but also let me explain. I am making a player selection screen where you will select from multiple characters. I all ready have a plan for the bulk of the code I am stuck right in the beginning.
Basically I made four guitextrues as of right now and want to detect when they are pressed from a script attached to a plane. I don't want to do individual scripts for each guittexture and getcomponent and crap obviously. Am I doing this the hard way, please let me know and thank you in advance!
Answer by aronatvw · Feb 01, 2014 at 06:00 PM
I did not want to send message through all my code and honestly was not really understanding that method so I just wrote this code which works. All I have to do is change the public string to match it's relative method in the main script! It may be a bit barbaric maybe, but it works. Also I know getcomponent and invoke may be utilize a good amount of memory but its a very simple scene.
using UnityEngine;
using System.Collections;
public class GUIClick : MonoBehaviour {
private PlayerSelectionScreen playselecscreen;
public GameObject bg;
public string whattoinvoke;
void OnMouseDown(){
playselecscreen.Invoke(whattoinvoke, 0.0F);
}
// Use this for initialization
void Start () {
bg = GameObject.Find("PlayerSelectBG");
playselecscreen = bg.GetComponent<PlayerSelectionScreen>();
}
// Update is called once per frame
void Update () {
}
}
Answer by HuskyPanda213 · Feb 01, 2014 at 05:36 AM
Why do you use a GUI texture? Plus, I think you would have to make each texture have a script. The thing I would do is have a script called: GUISendMessage.cs, and have the code that follows.(for some reason it posted this when incomplete, but now it is complete)
using UnityEngine; using System.Collections;
public class GUISendMessage : MonoBehaviour {
public string SendMessageName = "SendMessageFunctionName";//This is the function called in the Main/Manager Class.
public GameObject SendMessageTo;//Place the Master Object in here
void OnMouseDown(){
SendMessageTo.SendMessage(SendMessageName);//If this gives an error use this SendMessageTo.SendMessage(SendMessageName,SendMessageOptions.DontRequireReceiver);This may is just for safety.
}
}
Then in the Main/Manager Class make a void called
void TheMessageThatWasSentFromTheTexture(){//Don't really call it that, call it what the name of the string SendMessageName was.
//Call stuff in here.
}
If you do not understand the premise of SendMessage I will try to explain. SendMessage calls a function by what it is named. The input string, in the case the SendMessageName variable this will make a function with the name given in send message call. The SendMessageTo.SendMessage(), part makes it that it is sent to the gameobject with the Management script that has the functions that need to be called inside. If needing further help:
http://docs.unity3d.com/Documentation/ScriptReference/GameObject.SendMessage.html
No I can figure that out, I was just hoping I could have detected the button clicked from the main script using public GUITexture.
So what do you recommend using as opposed to guitexture?