how to collect Pick Ups, open Door?

New to Unity and c#. Motivated and full of extra good karma to toss out to anyone who can help! :slight_smile:

So I built some double doors that slide open and closed when triggering a box collider using this tutorial. I have a Doors script attached to a DoorParent game object.

I also have six collectible Pick Ups (rotating cubes) that my Player can pick up. I have a PlayerMovement script attached to my Player that contains code which counts the number of picks ups collected.

I want to learn how to trigger events when a certain number of collectibles are picked up by the Player. So I am trying to create code that says if 3 Pick Ups are collected by Player, the doors slide open. Here’s the code I’ve got and I’m not sure where I’m going wrong. Right now the console is telling me: Assets/Scripts/PlayerMovement.cs(42,25): error CS1955: The member `PlayerMovement.DoorControl’ cannot be used as method or delegate

Doors

using UnityEngine;
using System.Collections;

public class Doors : MonoBehaviour {

    Animator animator;
    bool doorOpen;

    void Start()
    {
        doorOpen = false;
        animator = GetComponent<Animator>();
    }

    void OnTriggerEnter(Collider col)
    {
        if(col.gameObject.tag == "Player")
        {
            doorOpen = true;
            DoorControl ("Open");
        }
    }

    void OnTriggerExit(Collider col)
    {
        if (doorOpen)
        {
            doorOpen = false;
            DoorControl ("Close");
        }
    }

    void DoorControl(string direction)
    {
        animator.SetTrigger(direction);
    }
}

PlayerMovement

using UnityEngine;
using System.Collections;

public class PlayerMovement : MonoBehaviour {

    public float speed;
    public GameObject DoorParent;
    private int count;
    Animator animator;
    bool doorOpen;
   
    private Rigidbody rb;
   
    void Start ()
    {
        rb = GetComponent<Rigidbody>();
        count = 0;
        doorOpen = false;
        animator = DoorParent.GetComponent<Animator>();
    }
   
    void FixedUpdate ()
    {
        float moveHorizontal = Input.GetAxis ("Horizontal");
        float moveVertical = Input.GetAxis ("Vertical");
       
        Vector3 movement = new Vector3 (moveHorizontal, 0.0f, moveVertical);
       
        rb.AddForce (movement * speed);
    }

    void OnTriggerEnter(Collider other)
    {
        if (other.gameObject.CompareTag ("Pick Up"))
        {
            other.gameObject.SetActive (false);
            count = count + 1;
        }
        if (count == 3) 
        {
            doorOpen = true;
            DoorControl ("Open");
        }
    }
}

Do you want the doors to open as soon as the player has collected 3 items or the trigger to only work when they have 3 items?
Currently the doors script has no reference to the bool on the player so you need to create a reference to the player in your door script so you can access the public bool on the player. What you have actually done is create 2 different bools with the same name, one in each script.
How to access variables on different scripts is asked a lot & is covered in lots of places by text & video so I suggest you check a couple of them out as each explains it slightly differently & everyone learns slightly differently.

1 Like

Following up on @tedthebug bug’s posting, everything he said for sure, but I would add this:

In the door script, give it a public array of GameObjects that you would fill out (in the editor) with the individual powerups.

Then in the Door’s Update() function, scan that list and see how many of them have been destroyed (because they have been collected) and you can judge whether the door should open or not.

F’rinstance, if you have SIX collectable objects, but you want the door to open after any THREE have been collected, then in your update you would iterate the entire array looking for GameObjects that have been destroyed, tally up how many are destroyed, amd decide to open the door.

If you have a GameObject variable called foo, you can simply say:

if (!foo) {
// then this game object has been Destroyed
}
1 Like

Couldn’t you just implement a public counter on the player script and have the powerup increment it every time you go through a powerup?