Code errors

Please help me with this code, this error is currently stopping my project

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

[RequireComponent(typeof(Rigidbody2D))]
public class TapController : MonoBehaviour {

public float tapForce = 10;
public float tiltSmooth = 5;
public Vector3 startPos;

Rigidbody2D Rigidbody;
Quaternion downRotation;
Quaternion forwardRotation;

private void Start() {
Rigidbody = GetComponent();
downRotation = Quaternion.Euler(0, 0, -90);
forwardRotation = Quaternion.Euler(0, 0, 35);

}

private void Update() {
if (Input.GetMouseButtonDown(0))
{
transform.rotation = forwardRotation;
Rigidbody.AddForce(Vector2.up * tapForce, ForceMode2D.Force);
}

transform.rotation = Quaternion.Lerp(transform.rotation, downRotation, tiltSmooth * Time.deltaTime);
}
}

void OnTriggerEnter2D(Collider2D col){
if (col.gameObject.tag == ā€œscoreZoneā€)
{
// register a score event
// play a sound
}
if (col.gameObject.tag == ā€œdeadZoneā€)
{
Rigidbody.simulated = false; (Rigidbody does not contain a definition for `simulated“)
//register a dead event
//play a sound
}

}

}

I realized that the first error was a typo so that’s corrected. Still don’t know anything about the second error though.

Check the first post in this forum about formatting your code to make it readable.

The error is exactly what it says. You don’t have any variable named ā€œtapForceā€ defined in your script. I’m guessing your variable tapForence is the one you meant to use.

Thanks for that. Do you know anything about the other error?

Rigidbody is a class in Unity, so my guess is it’s getting confused and thinks you want the class and not the variable you defined. It’s bad practice to name variables after classes. Change the variable to name to rigidbody at the very least.

Thanks for the tip. I tried changing to lowercase r, but it didnt seem to help. When I changed it the error message said " the name ā€˜rigidbody’ does not exist in the current context" instead…

Well…if you change it in one place, you have to change it in all places that you use it at…

I did that as well, still lots of errors…

Post your updated script with your changes. And use code tags this time.

Like this?

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;

[RequireComponent(typeof(Rigidbody2D))]
public class TapController : MonoBehaviour {

    public delegate void PlayerDelegate();
    public static event PlayerDelegate OnPlayerDied;
    public static event PlayerDelegate  OnPlayerScored;

    public float tapForce = 10;
    public float tiltSmooth = 5;
    public Vector3 startPos;

    Rigidbody2D rigidbody;
    Quaternion downRotation;
    Quaternion forwardRotation;

    GameManager game;

    void Start() {
        rigidbody = GetComponent<Rigidbody2D>();
        downRotation = Quaternion.Euler(0, 0, -90);
        forwardRotation = Quaternion.Euler(0, 0, 35);
        game = GameManager.Instance;
        rigidbody.simulated = false;
    }

    void OnEnable()
    {
        GameManager.OnGameStarted += OnGameStarted;
        GameManager.OnGameOverConfirmed += OnGameOverConfirmed;
    }

    void OnDisable()
    {
        GameManager.OnGameStarted -= OnGameStarted;
        GameManager.OnGameOverConfirmed -= OnGameOverConfirmed;
    }

    void OnGameStarted()
    {
        rigidbody.velocity = Vector3.zero;
        rigidbody.simulated = true;
    }

    void OnGameOverConfirmed()
    {
        transform.localPosition = startPos;     
        transform.rotation = Quaternion.identity;
    }
    void Update() {
        if (game.GameOver) return;

        if (Input.GetMouseButtonDown(0))
        {
            transform.rotation = forwardRotation;
            rigidbody.velocity = Vector3.zero;
            rigidbody.AddForce(Vector2.up * tapForce, ForceMode2D.Force);
        }

        transform.rotation = Quaternion.Lerp(transform.rotation, downRotation, tiltSmooth * Time.deltaTime);
    }
}

   void OnTriggerEnter2D(Collider2D col){
if (col.gameObject.tag == "scoreZone"){
        // register a score event
        OnPlayerScored(); //event sent to GameMananger;
        // play a sound
    }
if (col.gameObject.tag == "deadZone")
    {
        rigidbody.simulated = false;
        //register a dead event
        OnPlayerDied(); // event sent to GameMananger;
        // play a sound
    }
}
}

That looks much better. I realize you’re new, but it’s always helpful to post the exact error message and the line it’s talking about (also the line that matches in your post, if it differs from the one in the actual message*).
:slight_smile:

You may have an extra closing brace ā€˜}’ below the Update() method.

Thanks a lot! Removing the extra ā€˜}’ fixed it.

Some new errors popped up as I created another script;
Row 6: ā€œThe namespace ā€˜ā€™ already contains a definition for ā€˜GameManagerā€™ā€
Row 41 and 48: ā€œThe name ā€˜OnPlayerDied’ does not exist in the current contextā€

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;

public class GameManager : MonoBehaviour {

    public delegate void GameDeligate();
    public static event GameDeligate OnGameStarted;
    public static event GameDeligate OnGameOverConfirmed;

    public static GameManager Instance;

    public GameObject startPage;
    public GameObject gameOverPage;
    public GameObject countdownPage;
    public Text scoreText;

    enum PageState
    {
        None,
        Start,
        GameOver,
        Countdown
    }

    int score = 0;
    bool gameOver = true;

    public bool GameOver { get { return gameOver; } }
    public int Score { get{return score;} }

    void Awake()
    {
        Instance = this;
    }

    void OnEnable()
    {
        CountdownText.OnCountdownFinished += OnCountdownFinished;
        TapController.OnPlayerDied += OnPlayerDied;
        TapController.OnPlayerScored += OnPlayerScored;
    }

    void OnDisable()
    {
        CountdownText.OnCountdownFinished -= OnCountdownFinished;
        TapController.OnPlayerDied -= OnPlayerDied;
        TapController.OnPlayerScored -= OnPlayerScored;
    }

    void OnCountdownFinished()
    {
        SetPageState(PageState.None);
        OnGameStarted(); //event sent TapController
        score = 0;
        gameOver = false;
    }

    void OnPlayedDied()
    {
        gameOver = true;
        int savedScore = PlayerPrefs.GetInt("HighScore");
        if (score> savedScore)
        {
            PlayerPrefs.SetInt("HihgScore", score);
        }
        SetPageState(PageState.GameOver);
    }

    void OnPlayerScored()
    {
        score++;
        scoreText.text = score.ToString();
    }

    void SetPageState(PageState state)
    {
        switch (state)
        {
            case PageState.None:
                startPage.SetActive(false);
                gameOverPage.SetActive(false);
                countdownPage.SetActive(false);

                break;
            case PageState.Start:
                startPage.SetActive(true);
                gameOverPage.SetActive(false);
                countdownPage.SetActive(false);
                break;
            case PageState.GameOver:
                startPage.SetActive(false);
                gameOverPage.SetActive(true);
                countdownPage.SetActive(false);
                break;
            case PageState.Countdown:
                startPage.SetActive(false);
                gameOverPage.SetActive(false);
                countdownPage.SetActive(true);
                break;
        }
    }

    public void ComfirmGameOver()
    {
        //activated when replay button is hit
       OnGameOverConfirmed(); //event sent TapController
        scoreText.text = "0";
        SetPageState(PageState.Start);
    }

    public void StartGame()
    {
        //activated when playbutton is hit
        SetPageState(PageState.Countdown);

    }
}

Well, the first one is obvious. You have another script by that name. :slight_smile:
the second one is a subtle typo: OnPlayedDied vs. OnPlayerDied.

Just take a moment to look it over when you get an error. :slight_smile:

1 Like

Thanks for your patience.
I dont really understand what you mean with the error on line 6. What do I have to do to fix it? I cant find another script on my computer with the same name if thats what you meant.

I am an absolute beginner and make some unnessecary errors, but hopefully I will learn from them.

Ya, it’s no trouble. Just be sure to check the filename but also the script names (like when opened in your code editor).
You should always rename the file and the class name, if you do. Perhaps you renamed a file but forgot to do so in the class name?