Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 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 4shm3x · May 04, 2013 at 02:00 AM · randomimagescommands

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);
 }
 }
Comment
Add comment · Show 5
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 trs9556 · May 04, 2013 at 03:54 AM 0
Share

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?

avatar image trs9556 · May 04, 2013 at 03:59 AM 0
Share

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.

avatar image 4shm3x · May 06, 2013 at 08:25 PM 0
Share

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

avatar image trs9556 · May 08, 2013 at 11:00 PM 0
Share

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.

avatar image 4shm3x · May 08, 2013 at 11:17 PM 0
Share

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 :)

0 Replies

· Add your reply
  • Sort: 

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

13 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

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


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