Getting compiler errors that I cannot seem to decipher.

I am creating an endless runner 2D game, and I am trying to incorporate this script, but I am getting compiler errors that I cannot seem to decipher. Maybe somebody can see something that I didn’t.

Compiler Error:

Script:

using UnityEngine;
  using UnityEngine.UI;
 
  public class ScoreManager : MonoBehaviour
  {
      // SerializeField is a way to see the privae variable in the inspector.
      // When coding it is a good idea to not let a variable be accessed by anything that
      // it doesn't need to be.  If it is only used in this class it should be private.
      [SerializeField]
      Text scoreText;
      [SerializeField]
      Text hiScoreText;
      [SerializeField]
      float scoreCount = 0;
      [SerializeField]
      float hiScoreCount = 0;
      [SerializeField]
      float pointsPerSecond = 2;
    
      // Assign the player to this in the inspector.
      [SerializeField]
      SimplePlatformScript player;
      const string sScore = "Score: ";
      const string sHighScore = "High Score: ";
 
      void Update()
      {
          if (scoreIncreasing)
              scoreCount += pointsPerSecond * Time.deltaTime;
 
          if (scoreCount > hiScoreCount)
          {
              hiScoreCount = scoreCount;
              // The high score only needs to be updated when the score is higher. Not every frame.
              UpdateHighScoreText();
          }
          UpdateScoreText();
      }
 
      public void ResetPlayerScore()
      {
          scoreCount = 0;
          scoreText.text = "Score: 0";
       }
            
      void UpdateScoreText()
      {
          scoreText.text = string.Format("{0} {1}", sScore, Mathf.Round(scoreCount));
      }
 
      void UpdateHighScoreText()
      {
          hiScoreText.text = string.Format("{0} {1}", sHighScore, Mathf.Round(hiScoreCount));
      }
  }
 
  public class SimplePlatformScript : MonoBehaviour
  {
      //[HideInInspector] public bool facingRight = true;
      [HideInInspector] public bool jump = false;
      //public float moveForce = 365f;
      //public float maxSpeed = 5f;
      public float jumpForce = 1000f;
 
      public Transform groundCheck;
      // Store the layer in this reference so it doesnt have to create a new reference
      // every physics frame.
      [SerializeField]
      LayerMask whatIsGround;
 
      // Changed this to a public so the ScoreManager can see it.
      [SerializeField]
      public bool grounded = false;
 
      //private Animator anim;
      private Rigidbody2D rb2d;
      // Use const string anytime you are calling a string more than once.
      // Everytime you call a literal string it puts a new one in memory. 
      // Doing it like this make it only create one.
      const string sJump = "Jump";
      // Use this for initialization
      void Awake()
      {
          //anim = GetComponent<Animator>();
          rb2d = GetComponent<Rigidbody2D>();
      }
 
      void Update()
      {
          if (Input.GetButtonDown(sJump) && grounded)
          {
              // This is ok to do in update as it feels better to the player.
              // This is a physics action but doing an input actions
              // feels off to the player if there is a delay.
              rb2d.AddForce(new Vector2(0f, jumpForce));
          }
      }
 
      void FixedUpdate()
      {
          // Any physics calculations are better done in the FixedUpdate.
          grounded = Physics2D.Linecast(transform.position, groundCheck.position, whatIsGround);
      }
  }

Tells you exactly what you need to know.

On line 28 you use a variable scoreIncreasing which is never declared.

In a script you have written but not provided code for called Caller you also have an error. You have a reference to a ScoreManager Instance, on line 26 and have tried to call a method called EnableScoring on it. We can see this method does not exist due to the code you have provided.

1 Like

Okay, I fixed the other script. Was actually a rather easy fix.

Here is the caller script. This is the one truly messing me up.

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

public class Caller : MonoBehaviour
{

    ScoreManager _sm;
    void Start()
    {
        // Get reference to ScoreManager in scene
        _sm = GameObject.Find("ScoreManager").GetComponent<ScoreManager>();
    }

    private void OnCollisionEnter2D(Collision2D collision)
    {
        if (collision.gameObject.layer == LayerMask.NameToLayer("Player"))
        {
            _sm.ResetPlayerScore(); // sample example of calling a method written in your ScoreManager
        }
    }
    private void OnCollisionExit2D(Collision2D collision)
    {
        if (collision.gameObject.layer == LayerMask.NameToLayer("Player"))
        {
            _sm.EnableScoring();
        }
    }
}

As the error said on line 26 you’re calling _sm.EnableScoring(), bu EnableScoring does not exist

Could I do Something like

void ScoreManager.EnableScoring()

You could just remove line 26 if you’re going to make that function call do nothing anyway. I’m taking it this isn’t your code originally?

otherwise this is the definition you need in score manager.

public void EnableScoring()
{
    //code here
}

You should probably read up a few tutorials on coding if you’re finding this difficult to debug.

Got it. Thank you!