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);
}
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.
// 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 :
why is it that the error appears on line 35 and 50?
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?
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);
}
}
}
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:
@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:
@ 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.
Could you please enlighten me on how to resolve such issues?
I am attaching the screenshot with 4 invalid arguments of die( transform)