- Home /
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.
It seems you added the same code twice. Also the errors asks about a "CollisionChecker.cs" file, is that the code you posted?
Sorry, I didn't notice I copied the same code twice. I just updated it.
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.
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.
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.
$$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
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# !
To certain extent, even C# can do that ;) For example with Lambdas or Anonymous $$anonymous$$ethods. But i'm not sure unity understands these.
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
Follow this Question
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