How do i make an object visible again after my player has collided with a pick up object

Hello,

Im fairly new to this whole C# deal so im probably doing something obviously wrong but it’s been a couple of hours now.

Basically what i’ve got is i have a missing platform which once the player has picked up the coin will become visible as i have it set to setActive(false) upon startup. I keep getting “Object reference not set to an instance of an object”

This is my playerController Script:

using UnityEngine;
using System.Collections;

public class PlayerController : MonoBehaviour {

    private Rigidbody rb;
    public float speed;
    public float jumpSpeed;


    void Start () {
        rb = GetComponent<Rigidbody> ();
    }

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

    void Jump(){

        rigidbody.AddForce (Vector3.up * jumpSpeed);
    }

    void FixedUpdate(){
        float moveHorizontal = Input.GetAxis ("Horizontal");
        float moveVertical = Input.GetAxis ("Vertical"); 
 
        Vector3 movement = new Vector3 (moveHorizontal, 0.0f, moveVertical);

        rb.AddForce (movement * speed);

        if(Input.GetKeyDown("space")){
            Jump();
        }
    }

    void OnTriggerEnter(Collider other){
        other.gameObject.SetActive (false);
        GameObject missingFloor = GameObject.Find("Missing Platform");
        ActiveObjects activeObjects = missingFloor.GetComponent<ActiveObjects>();
        activeObjects.Visible ();

    }
}

And this is my Script for the missing platform:

using UnityEngine;
using System.Collections;

public class ActiveObjects : MonoBehaviour {



    void Start () {
        gameObject.SetActive (false);

    }



    public void Visible(){
        gameObject.SetActive (true);
    }
    void Update () {

    }
}

The error is as follows

NullReferenceException: Object reference not set to an instance of an object
PlayerController.OnTriggerEnter (UnityEngine.Collider other) (at Assets/Scripts/PlayerController.cs:47)

OK, first, please use code tags to make the code readable. Easiest way to do that is to use the Insert button, on the message toolbar between the movie filmstrip and the floppy disk.

Next: when you say you get an error, it’s important to point out which line the error is reported on. You can easily find this out by just double-clicking the error in the Unity console; it should take you right to it.

Just from looking at this script, there are a whole lot of things that could be null, so it’s really hard to guess without that information.

Thanks for the advice on how to post, ive made the changes you asked for and if you could take a look for me that would be awesome :slight_smile: Also the lines have skewed slightly since i copied them in so the error was from line 42/43

OK, for some reason the line numbers don’t quite line up, so we still don’t know exactly which line it was (but you do, because you double-clicked the error in the console, right?). But it’s somewhere in here:

void OnTriggerEnter(Collider other){
        other.gameObject.SetActive (false);
        GameObject missingFloor = GameObject.Find("Missing Platform");
        ActiveObjects activeObjects = missingFloor.GetComponent<ActiveObjects>();
        activeObjects.Visible ();
    }

This is short code, so let’s just examine each possibility.

  • other is null — not likely, as Unity only calls this method with a valid collider
  • other.gameObject is null — again, more or less impossible
  • missingFloor is null — this would be true if GameObject.Find fails to find an object with the given name; quite likely
  • activeObjects is null — this would be the case if we found a Missing Platform object, but it lacks an ActiveObjects component

Both 3 and 4 are quite likely things that could happen, and your code should check whether those are null before attempting to use them.

ActiveObjects activeObjects = missingFloor.GetComponent();
this is the line where the error is coming from now i have an object called MissingPlatform (i have since removed the space from both the object name in the hierarchy and in the code to remove any whitespace) i have even tried giving it a tag and searching for it by tag but still the same error
and my Missing platform object has 100% the script named ActiveObjects attached to it

Ive just used the Debug.Log and found that my missingFloor is null, how can this be the case if i seem to have done everything correctly? I do not get null however if i stick “Player” in there instead of MissingPlatform