c# "}"expected error?

Hello! I’m new to Unity3d and I would like to know why I am getting this error message as shown in the screenshot below:

// Die by collision
	void OnCollisionEnter2D(Collision2D other)

	{
		if (other.gameObject.CompareTag("food"));
			Destroy (other.gameObject);

	else 
			Die();
		Application.LoadLevel(Application.loadedLevel);
	}

also, the full code of my character seems perfectly fine. I’m attaching it below for your reference.

using UnityEngine;
using System.Collections;

public class runner : MonoBehaviour {


	// The force which is added when the player jumps
	// This can be changed in the Inspector window
public Vector2 jumpForce = new Vector2(0, 1);



	// Use this for initialization
	void Start () {
			

	
	}

	// Update is called once per frame
	void Update () {


	
		// Jump
		if (Input.GetKeyUp("space"))
		{
			rigidbody2D.velocity = Vector2.zero;
			rigidbody2D.AddForce(jumpForce);
		}
		// Die by being off screen
		Vector2 screenPosition = Camera.main.WorldToScreenPoint(transform.position);
		if (screenPosition.y > Screen.height || screenPosition.y < 0)
		{
			Die();
		}
	}

	// Die by collision
	void OnCollisionEnter2D(Collision2D other)

	{
		if (other.gameObject.CompareTag("food"));
			Destroy (other.gameObject);

	else 
			Die();
		Application.LoadLevel(Application.loadedLevel);
	}

Could anyone please tell me what’s wrong?

check your IF statement in the OnCollisionEnter2D you need to remove the semi colon and it should work :wink: ! good luck!

1 Like

try putting both the lines after else inside bracket
{

if (other.gameObject.CompareTag(“food”))

Destroy (other.gameObject);

else
{
Die();

Application.LoadLevel(Application.loadedLevel);
}

}
hope this will solve it

use the braces to encompass the actions you want based on the conditions. You can get away with:

if(condition)action;

if you only want a single instruction to be based on the condition, but it’s the same as doing

if(condition)
{
action;
}

and you’ll probably find the later is easier to read once the code gets bigger/more complex.

semicolons ; indicate an end of an instruction, so writing

if(condition);

is basically a pointless line, “if the condition is true, … … end of line, next”

From what I see, you are missing the last “}” in your code, for the class runner.
Other than that, your “if” condition is strange, as others have mentioned.

Hello everyone!

Thanks for your informative responses! :smile:

However, the same error persists after I added a } with the corrections suggested.

This is the screenshot of the error message:

http://i58.tinypic.com/11hufra.jpg

May I know why is this happening again?

if you look at the line 44 were it says Destroy(other.gameObject};

you need to change the curly brackets to a normal one there for it will be

Destroy(other.gameObject);

also the last bracket should be changed to a curly bracket

this would fix the problem :smile:

good luck :wink:

I still think you need one last “}”.

From the line 39, the rest would be:

    // Die by collision
    void OnCollisionEnter2D(Collision2D other)
    {
        if(other.gameObject.CompareTag("food"))
        {
            Destroy(other.gameObject);
        }
        else
        {
            Die();
            Application.LoadLevel(Application.loadedLevel);
        }
    }
} // <-- this one closes the class

I followed your suggestion but it only made things worst. The next error appears on the" // Die by being offscreen " part.
I find it bizarre since there’s nothing wrong with code in there .

I am attaching a screenshot for your reference :

1639520--101582--$2ur9oyh.png

why is it that the error appears on line 35 and 50? :frowning:

Because no method named Die() exists within the scope of your class.

^ Exactly what he said. Try making your Die() method, static, even though I would warn against this.

@motionlife89 Can you show us the error message, (the one shown in the console (in Unity)), and can you show us this mysterious Die() method?

From what I understand from your code, this error only started now because there was an error from syntax (missing “}”), now that the code is written right, it could not find the Die(); method.

I don’t see this method in your code, what does this Die() do?

It kills the object I guess.

Copy paste the vote that is below. Seems like it may be of some help.

using UnityEngine;

using System.Collections;

 

public class runner : MonoBehaviour {

 

 

    // The force which is added when the player jumps

    // This can be changed in the Inspector window

public Vector2 jumpForce = new Vector2(0, 1);

 

 

 

    // Use this for initialization

    void Start () {

            

 

    

    }

 

    // Update is called once per frame

    void Update () {

 

 

    

        // Jump

        if (Input.GetKeyUp("space"))

        {

            rigidbody2D.velocity = Vector2.zero;

            rigidbody2D.AddForce(jumpForce);

        }

        // Die by being off screen

        Vector2 screenPosition = Camera.main.WorldToScreenPoint(transform.position);

        if (screenPosition.y > Screen.height || screenPosition.y < 0)

        {

            Die();

        }

    }

 

    // Die by collision

    void OnCollisionEnter2D(Collision2D other)

 

    {

        if (other.gameObject.CompareTag("food"));
     {

            Destroy (other.gameObject);
      }

    else 
       {

            Die();

        Application.LoadLevel(Application.loadedLevel);
        }
    }
  }

His code is missing the method, maybe it is like this?

void Die()
{
    Destroy(this.gameObject);
}

If I understand correctly, I should make the Die () static by
writing it this way?

I’m not really sure how to go about it.anyway, the Die () function is to make the runner die when it hits an obstacle and when it goes off screen. the method will also cause the game to reset or restart.

also, it must destroy the object with a tag “food” when it collides with it.

as requested. I am attaching the error in the Unity console:

Moreover, I tried to use @zDemonhunter99’s suggestion and this is the error I got during the debugging
process:

http://i59.tinypic.com/ivy7vk.png

@maikonfarias: I’m not sure where to insert your suggestion either. I tried insert it after the else
statement. Unfortunately, an error occurred. I am attaching a 2 screenshots for your reference:

1.)

2.) http://i59.tinypic.com/donxaw.jpg

Did you maybe think that Die() gets automatically called without passing any logic?

You have to specify it like this:
Code:
void Die()

{

Destroy(this.gameObject);

}

Read some basic tutorials. Like functions.

using UnityEngine;
using System.Collections;

public class runner : MonoBehaviour {	

	public Vector2 jumpForce = new Vector2(0, 1);

	void Update () {

		if (Input.GetKeyUp("space")) {			
			rigidbody2D.velocity = Vector2.zero;			
			rigidbody2D.AddForce(jumpForce);			
		}
		
		Vector2 screenPosition = Camera.main.WorldToScreenPoint(transform.position);
		
		if (screenPosition.y > Screen.height || screenPosition.y < 0)		
			Die(transform);
	}

	void OnCollisionEnter2D (Collision2D other) {

		if (other.gameObject.tag == "food")
			Destroy (other.gameObject);

		else 
			Die(transform);
	}

	public static void Die (GameObject gameobject) {
		DestroyObject(gameobject);
		Application.LoadLevel(Application.loadedLevel);
	}
}

Maybe the code from @djfunkey might work for you, if not, I suggest you to study some script tutorials.

Unity - Scripting Lessons:
http://unity3d.com/learn/tutorials/modules/beginner/scripting

@ djfunkey Thank you very much for the suggestion! However, it seems there are errors. I have never encountered them before. I tried searching for the remedy with people having the same problem as mine but no luck. :frowning:

Could you please enlighten me on how to resolve such issues?

I am attaching the screenshot with 4 invalid arguments of die( transform)

http://i60.tinypic.com/3h1ev.jpg

This is the error from the Unity console for additional info:

http://i61.tinypic.com/vynmuv.png