0

I have 2 files/scripts of code:

the first one:

using System.Collections.Generic;
using UnityEngine;

public class MoveLeft : MonoBehaviour
{
    private float Speed = 15f;
    private PlayerController playerControllerScript;
    // Start is called before the first frame update
    void Start()
    {
        playerControllerScript = GameObject.Find("Player").GetComponent<PlayerController>();
    }

    // Update is called once per frame
    void Update()
    {
        if(playerControllerScript.gameOver == false)
        {
            transform.Translate(Vector3.left * Time.deltaTime * Speed);
        }

        //transform.Translate(Vector3.left * Time.deltaTime * Speed);

    }

}

the second one:

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

public class PlayerController : MonoBehaviour
{
    private Rigidbody playerRB;
    public float JumpForce = 150;
    public float GravityModifier;
    public bool OnTheGround = true;
    public bool gameOver;
    // Start is called before the first frame update
    void Start()
    {
        playerRB = GetComponent<Rigidbody>();
        Physics.gravity *= GravityModifier;
    }

    // Update is called once per frame
    void Update()
    {
        if (Input.GetKeyDown(KeyCode.Space)&& OnTheGround)
        {
            playerRB.AddForce(Vector3.up * JumpForce, ForceMode.Impulse);
            OnTheGround = false;
        }
    }
    private void OnCollisionEnter(Collision collision)
    {
        
        if (collision.gameObject.CompareTag("Ground"))
        {
            OnTheGround = true;
        }
        
        if (collision.gameObject.CompareTag("Obsticle"))
        {
            Debug.Log("Game over");
            gameOver = true;
        }
        
    }
}

So for my simple game, I need to check if the game over 'bool' is checked or not(true or false) then send the bool value to the first script. I have already tried this but I am repeatedly getting an error.

Please help me. I am not very good at programming.

TEEBQNE
  • 6,104
  • 3
  • 20
  • 37
Scarlett
  • 25
  • 3
  • What is the error you are getting? Can you post it? – TEEBQNE May 13 '21 at 00:05
  • NullReferenceException: Object reference not set to an instance of an object MoveLeft.Update () (at Assets/Course Library/scripts/MoveLeft.cs:18) – Scarlett May 13 '21 at 00:25

2 Answers2

0

Based on the error you posted in the comments, there is a null reference in the Update method of your script MoveLeft.cs. The one reference that can be nullable in your Update is playerControllerScript. The error is specifying that the reference for playerControllerScript is not assigned, so it is null, meaning that accessing it will result in an error.

The line you are using to grab the reference, in theory, is correct:

GameObject.Find("Player").GetComponent<PlayerController>();

You are searching for an object named Player that has the script component called PlayerController.cs. The reference is not found due to no object in your scene being called Player or the object called Player does not have the script component PlayerController.cs attached to it. The object called Player must be in the same scene as your other object and it must have this script component attached. If a child object has the script, you can use Component.GetComponentInChildren.

TEEBQNE
  • 6,104
  • 3
  • 20
  • 37
  • Good answer. Just to pinpoint something. It's true that the object need to be in the same scene, but you can preserve objects from one scene to another by calling DontDestroyOnLoad on each object you want to preserve. This will "carry" object to any scene and "Find" method would work. – rootpanthera May 13 '21 at 05:25
-1

In order to access the values of a class you need to instanciate an object of said class. Then you can access said values using it´s respective get and set methods.

Pepinho
  • 11