- Home /
How to specify which image to load as a command for the player
In my game there will be images that pop up, these images are commands for the player to perform a certain action.
Right now I have it set as text for debug until my images were sorted. I have them now but I am unsure how to make it so. Here is my code with the text
using UnityEngine;
using System.Collections;
public class Commands : MonoBehaviour {
public float Speed = 0.5f;
public static int Command = 0;
public float TimeLeft = 0;
void Start (){
Command = Random.Range(0,5);
}
void Update (){
TimeLeft += Time.deltaTime;
if(TimeLeft >2)
{
Respawn();
}
transform.Translate(Vector3.up * Time.deltaTime * Speed); // Translate it up
if(transform.position.y >= 1.65f) // Respawn it
{
Respawn();
}
switch(Command)
{
case 1:
guiText.text = "Mouth";
break;
case 2:
guiText.text = "Eye";
break;
case 3:
guiText.text = "Nose";
break;
case 4:
guiText.text = "Ear";
break;
}
}
public void Respawn (){
TimeLeft = 0;
transform.position = new Vector3(Random.Range(0.2f,0.9f), 0.4f, transform.position.z);
Command = Random.Range(1,5);
}
}
Can you be a bit more specific when you say "I am unsure how to make it".
Is your current code working in the way you want? Are you having trouble translating the text to images?
If your code isn't working the way you want it to, how is it different from what you want?
After rereading your question it appears you just want to convert it to show images rather than text.
With that being said my suggestion is to rethink how you are going about this. It appears you are using a guiText.
I would suggest migrating this into a OnGUI() and then displaying the image that way.
So for example
//these go up at the top, declaring your variables
public Texture2D pictureOf$$anonymous$$outh; //set to the picture of your mouth in the inspector
public Texture2D pictureOfEye; //set to the picture of your eye in the inspector
private Texture2d myImage; //this will be set in code.
void OnGUI(){
GUI.drawText(new Rect(0,0,100,100), myImage);
}
And in your update method you can do just like you have but with images
switch(command){
case 1:
myImage = pictureOf$$anonymous$$outh;
break;
case 2:
myImage = pictureOfEye;
break;
}
This idea that is.
Sounds logical, only problem is (i should of mentioned earlier is that this is for a mobile game). ONGui uses a lot of power on the mobile devices
It does use a lot of power on mobile devices. I suggest adding a frames per second script to see what you get, implement this and see if it drops any.
If it does drop enough to effect performance then perhaps you should look into using a third party gui system.
I use OnGUI in my android development and although it does impact my performance I have not had a problem with it to the extent of it ruining my game.
Just remember OnGUI is called twice per frame, so don't do any heavy calculations or fetching. $$anonymous$$ake a variable that gets updated in the Update() function, and have OnGUI just print out the value.
Forgot to reply to this but I tried it out also with an FPS script in the scene and am getting 55fps on a low end android device with the OnGUI. SO not bad at all!. Thanks for your help :)
Your answer
Follow this Question
Related Questions
Multiple Cars not working 1 Answer
Changing Objects Material Using My Array 1 Answer
Help with spawning an object in a random location 1 Answer
random name selector 3 Answers
Choose random background at start 1 Answer