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 NightWarrior · Dec 11, 2014 at 07:57 AM · trigger

Having problems when using triggers

Hello all, am having a problem when I want use the OnTriggerEnter method. So basically I have a list of information(questions) and I want to get a random question every time when the player enters the trigger, but I get spammed all the questions really fast instead. I didn't manage to solve this problem so here is my code. I will be really thankful if somebody helps me.

 public int width = 800;
         public int height= 300;
         public bool showWindow = false;
         public bool gameOver = false;
         public System.Collections.Generic.List <question> questions;
         [Space(10)]
         private playerController pc;
         //private string countryName;
 
         //private int score = 0;
         private int randomNumber;
 
         void Start () {
             pc = GetComponent<playerController> ();
         }
 
         void OnGUI () {
             /*if (Input.GetAxis ("Q") > 0) {
                 randomNumber = Random.Range (0, questions.Count);
                 showWindow = true;
             }*/
             if (showWindow) {
                 randomNumber = Random.Range (0, questions.Count);
             GUI.Window (0, new Rect (Screen.width / 2 - width/2, Screen.height / 2 - height/2, width, height), WindowFunction, questions [randomNumber].questionName);
                 Screen.lockCursor = false;
                 pc.lockScreen = true;
             }
         }
 
         private void OnTriggerEnter (Collider collider) {
             //this.countryName = collider.gameObject.name;
             if (collider.gameObject.name == "Trigger") {
                 showWindow = true;
             }
         }
 
         private void OnTriggerExit (Collider collider) {
             if (collider.gameObject.name == "Trigger") {
                 showWindow = false;
                 pc.lockScreen = false;
                 Screen.lockCursor = true;
             }
         }
 
         private void rightAnswer () {
             int rnd = Random.Range (1, 50);
             int a = 0;
 
             switch (questions[randomNumber].difficulty) {
                 case 1: a += 100 + rnd;
                         break;
                 case 2: a += 300 + rnd;
                         break;
                 case 3: a += 700 + rnd;
                         break;
                 }
 
             pc.score += a;
             showWindow = false;
             pc.lockScreen = false;
             Screen.lockCursor = true;
         
         }
 
         private void wrongAnswer () {
             if (pc.lives > 0) { 
                 pc.lives -= 1;
                 showWindow = false;
                 pc.lockScreen = false;
                 Screen.lockCursor = true;
             }
 
             if (pc.lives <= 0) {
                     Application.LoadLevel ("Game over");
                     gameOver = true;
             }
         }
 
         private void WindowFunction (int windowID) {
             if (GUI.Button (new Rect (50, 20, 600, 30), "A) " + questions [randomNumber].answerA)) {
                     if (questions [randomNumber].answerRight == 1) {
                             rightAnswer ();
                     } else {
                             wrongAnswer ();
                     }
                 
             }
             
                 if (GUI.Button (new Rect (50, 55, 600, 30), "B) " + questions [randomNumber].answerB)) {
                     if (questions [randomNumber].answerRight == 2) {
                             rightAnswer ();
                     } else {
                             wrongAnswer ();
                     }
                 }
             
                 if (GUI.Button (new Rect (50, 90, 600, 30), "C) " + questions [randomNumber].answerC)) {
                     if (questions [randomNumber].answerRight == 3) {
                             rightAnswer ();
                     } else {
                             wrongAnswer ();
                     }
                 }
             
                 if (GUI.Button (new Rect (50, 260, 600, 30), "Exit)) {
                     showWindow = false;
                     pc.lockScreen = false;
                     Screen.lockCursor = true;
 
                 }
 
         }



Comment
Add comment
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

1 Reply

· Add your reply
  • Sort: 
avatar image
0
Best Answer

Answer by GameVortex · Dec 11, 2014 at 08:27 AM

You are generating your randomNumber in the OnGUI function. OnGUI is called by Unity multiple times each frame. This will generate a new random number every time and you get the effect you see, all the questions cycled through fast.

A quick fix can be to move the generation of the randomNumber to the OnTriggerEnter function, then the number will only be generated when the player enters the trigger.

Comment
Add comment · Show 1 · Share
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 NightWarrior · Dec 11, 2014 at 12:14 PM 0
Share

Thank you a lot, it worked :).

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

3 People are following this question.

avatar image avatar image avatar image

Related Questions

Multiple Cars not working 1 Answer

MouseOver Doesn't work ON trigger 3 Answers

unknown identifer explosion problem 2 Answers

Renaming a GameObject Clone 1 Answer

Where is the problem? 0 Answers


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