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 Razputin · Feb 20, 2014 at 09:37 PM · guitriggerrandomvoid

Why can't void be used in this context?

Why isn't void start() working here? Whats going wrong here?

 using System.Collections;
 
     public class DetectGridGrassland : MonoBehaviour {
 
     public int randNum = 0;
 
     void OnTriggerEnter(Collider other){
         Debug.Log("Player entered the Grass Land");
     
         void Start(){
             randNum = Random.Range(0,5);
         }
         
         void OnGUI(){
             if (randNum <= 3)
             {
                 GUI.Button(new Rect(10, 10, 125, 25), "Attacked by bandits");
             }
             else if(randNum == 3)
             {
                 GUI.Button(new Rect(10, 10+20, 125, 25), "Attacked by wolves");
             }
             else
             {
                 GUI.Button(new Rect(10, 10+30, 125, 25), "Nothing to do!");
             }
         }
     }
 }
 
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

2 Replies

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

Answer by ICHeeryI · Feb 20, 2014 at 09:58 PM

No you can not use void in void(method in method). You check if player entered grass land, but now you want to tell other methods that player entered grass land. Just create new variable

 bool enter = false;

So when player enter grass land set it to true, when he leave set it to false.

 using System.Collections;
  
     public class DetectGridGrassland : MonoBehaviour {
  
     public int randNum = 0;
     public bool enter = false;
     void OnTriggerEnter(Collider other){
        Debug.Log("Player entered the Grass Land");
        enter = true; //We entered grass land, set enter to true
      }
     void OnTriggerExit(Collider other){
         enter = false;//We left grass land, set enter to false
     }
        void Start(){
          randNum = Random.Range(0,5);
        }
  
        void OnGUI(){
         //Check if enter is true
         if(enter == true){
          if (randNum <= 3)
          {
           GUI.Button(new Rect(10, 10, 125, 25), "Attacked by bandits");
          }
          else if(randNum == 3)
          {
           GUI.Button(new Rect(10, 10+20, 125, 25), "Attacked by wolves");
          }
          else
          {
           GUI.Button(new Rect(10, 10+30, 125, 25), "Nothing to do!");
          }
        }
     }
 }
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 Razputin · Feb 20, 2014 at 10:09 PM 0
Share

This worked perfectly. Thank you for explaining this to me!

avatar image
0

Answer by CodeElemental · Feb 20, 2014 at 09:38 PM

Missing closing bracket for this method :

  void OnTriggerEnter(Collider other){
        Debug.Log("Player entered the Grass Land");
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 flaviusxvii · Feb 20, 2014 at 09:48 PM 0
Share

It's not missing.. (but Start and OnGUI are declared inside it)

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

21 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 avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

triggering random animations with gui 1 Answer

How to get random GUI to appear, on entering a trigger. (PIC INCLUDED) 1 Answer

GUI is not staying where it should 1 Answer

Can I set a specific Pointer for the EventTrigger on Buttons for example? 0 Answers

How do I use a trigger to change the GUIText 2 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