Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 11 Next capture
2021 2022 2023
1 capture
11 Jun 22 - 11 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 $$anonymous$$ · Jul 06, 2015 at 02:51 PM · functionmethod

Does Unity not like methods inside of other ones?

In Unity 5, I am trying to create a simple character controller for a cube. I have a plane parented to the bottom of the cube to check for collisions with the ground to reset a "grounded" bool variable. The compiler says: CollisionChecker.cs(11, 45) Keyword 'void' cannot be used in this context CollisionChecker.cs(11, 46) Unexpected symbol '(' CollisionChecker.cs(16, 1) Parsing error Here are the 2 pieces of code I have, one directly on the cube and the other on the parented plane. Cube's Code:

 using UnityEngine;
 using System.Collections;
 
 public class PlayerController : MonoBehaviour {
     public Rigidbody player;
     public float moveBackwardMultiplier = 1;
     public float moveForwardMultiplier = 1;
     public float moveSidewaysMultiplier = 1;
     public float jumpForce;
     public float jumpMultiplier = 1;
     public bool grounded = true;
     void Jump () {
     while(grounded){
         if(Input.GetButtonDown("Jump")){
             grounded = false;
             Vector3 vectorJump = new Vector3 (0, jumpForce, 0);
             player.AddForce(vectorJump * jumpMultiplier);
     }    
     }
     }
     void Start() {
         player = gameObject.GetComponent<Rigidbody> ();
     }
 
 
 
 
 }

Plane's Code:

 using UnityEngine;
 using System.Collections;
 
 public class CollisionChecker : MonoBehaviour {
     public PlayerController playerController;
     void Start() {
         playerController = gameObject.GetComponentInParent<PlayerController> ();
     }
     void FixedUpdate() {
         while(!playerController.grounded){
             void OnCollisionEnter(Collider other){
                 playerController.grounded = true;
             }
         }
     }
 }

   

If anyone could help me out with why my code is not working I would really appreciate it or even more so if they told me a better way of doing it than the way I am now.

Comment
Add comment · Show 3
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 Nischo · Jul 06, 2015 at 03:11 PM 0
Share

It seems you added the same code twice. Also the errors asks about a "CollisionChecker.cs" file, is that the code you posted?

avatar image $$anonymous$$ · Jul 06, 2015 at 03:51 PM 0
Share

Sorry, I didn't notice I copied the same code twice. I just updated it.

avatar image DennisDuty · Jul 06, 2015 at 04:12 PM 0
Share

I'm new to coding so I suck with errors and syntax and methodology. However having 'grounded' be a toggle like that may not be the best solution to begin with.

What happens if your cube gets to the edge of a platform and falls off? "grounded" will still be true, even though you'd be mid-air and you will be able to jump.

2 Replies

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

Answer by FaizanDurrany · Jul 06, 2015 at 04:15 PM

Hello Just_Alerik,

i don't think unity allows a function in a function but u can still achieve a similar result with this:

  using UnityEngine;
  using System.Collections;
  
  public class CollisionChecker : MonoBehaviour {
      public PlayerController playerController;
      
      private bool inTrigger = false;
 
      void Start() {
          playerController = gameObject.GetComponentInParent<PlayerController> ();
      }
      void FixedUpdate() {
          while(!playerController.grounded){
              if(inTrigger){
                 playerController.grounded = true;
              }
          }
      }
 
      void OnCollisionEnter(Collider other){
           inTrigger = true;
      }
  }

Regards Faizan.

Comment
Add comment · Show 2 · 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 $$anonymous$$ · Jul 06, 2015 at 04:35 PM 0
Share

I was trying to do it the way I did to only check when it needed to to optimize the game better, I guess I could always go back and fix it if needed. Thank you for you help.

avatar image Hellium · Jul 06, 2015 at 05:01 PM 1
Share

$$anonymous$$eep this in $$anonymous$$d when program$$anonymous$$g

Premature optimization is the root of all evil

Don't forget to accept @FaizanDurrany 's answer

avatar image
2

Answer by Hellium · Jul 06, 2015 at 03:25 PM

For sure you can't declare a function inside an other one ! You can just call it !

I don't know many languages allowing that. Python, I think, but not C# !

Comment
Add comment · Show 2 · 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 Nischo · Jul 06, 2015 at 03:36 PM 0
Share

To certain extent, even C# can do that ;) For example with Lambdas or Anonymous $$anonymous$$ethods. But i'm not sure unity understands these.

avatar image Hellium · Jul 06, 2015 at 03:40 PM 0
Share

Well, indeed, you are not totally wrong, but it's not a "normal" declaration with access modifiers, return type, ... Though, when you add Lambdas or Anonymous $$anonymous$$ethods to delegates / events, you create a "pointer" to a function (which you declare straightaway)

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

4 People are following this question.

avatar image avatar image avatar image avatar image

Related Questions

Function call is not working 0 Answers

Get the Caller of a static function? 1 Answer

Is it possible to assign custom functions to a Scriptable Object? 3 Answers

while loop not looping 2 Answers

Game is skipping over a called function and I'm not sure why 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