Home > Net >  Chest script calling another object not referenced in script
Chest script calling another object not referenced in script

Time:12-03

trying to have two chests (one for only dropping coins and one for coin and other items). When I press the button to open the chest, the small one opens and set the smallChestOpen to true, plays the animation and drops the coins. When I do the same thing on the big chest, it opens the small one. They have different scripts and are not reference in one another. When I open the small and go to the to big to open it, it does nothing. help!

Small Chest Code:

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

public class Chest : MonoBehaviour
{

    protected bool smallChestOpen = false;

    [SerializeField] private GameObject pressPanel;
    [SerializeField] private GameObject coinPrefab;
    [SerializeField] private GameObject[] coinToSpawn;

    [SerializeField] private int minCoinDrop = 5;
    [SerializeField] private int maxCoinDrop = 10;

    [SerializeField] private Animator animator;


    void Awake()
    {
        if(animator == null)
        {
            animator = GetComponentInParent<Animator>();
        }
    }

    private void Update()
    {
        if (PlayerInputManager.GetInstance().GetInteractPressed() && !smallChestOpen)
        {
            animator.SetBool("isOpen", true);
            SmallChestLootDrop();
        }
    }

    private void OnTriggerEnter2D(Collider2D collision)
    {
        if (!smallChestOpen)
        {
            if (collision.gameObject.CompareTag("Player"))
            {
                pressPanel.SetActive(true);

            }
        }


    }

    private void OnTriggerExit2D(Collider2D collision)
    {
        if (collision.gameObject.CompareTag("Player"))
        {
            pressPanel.SetActive(false);
        }
    }


    private void SmallChestLootDrop()
    {
        smallChestOpen = true;
        pressPanel.SetActive(false);

        int amount = Random.Range(minCoinDrop, maxCoinDrop);
        coinToSpawn = new GameObject[amount];
        for (int i = 0; i < coinToSpawn.Length; i  )
        {
            GameObject clone = Instantiate(coinPrefab, new Vector3(gameObject.transform.position.x, gameObject.transform.position.y, 0), Quaternion.identity);

            coinToSpawn[i] = clone;
        }
    }
}

Big chest Code:

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

public class ChestManyItems : MonoBehaviour
{
    private bool bigChestOpen = false;
    [SerializeField] private GameObject pressPanel;
    [SerializeField] private GameObject coinPrefab;
    [SerializeField] private GameObject[] coinToSpawn;

    [SerializeField] GameObject[] itemsToSpawn;

    [SerializeField] private int minCoinDrop = 5;
    [SerializeField] private int maxCoinDrop = 10;

    [SerializeField] private Animator animator;

    private void Awake()
    {
        if (animator == null)
        {
            animator = GetComponentInParent<Animator>();
        }
    }

    private void Update()
    {
        if (PlayerInputManager.GetInstance().GetInteractPressed() && !bigChestOpen)
        {
            animator.SetBool("isOpen", true);
            BigChestLootDrop();
        }
    }

    private void OnTriggerEnter2D(Collider2D collision)
    {
        if (!bigChestOpen)
        {
            if (collision.gameObject.CompareTag("Player"))
            {
                pressPanel.SetActive(true);

            }
        }


    }

    private void OnTriggerExit2D(Collider2D collision)
    {
        if (collision.gameObject.CompareTag("Player"))
        {
            pressPanel.SetActive(false);
        }
    }

    private void BigChestLootDrop()
    {
        bigChestOpen = true;

        int amount = Random.Range(minCoinDrop, maxCoinDrop);
        coinToSpawn = new GameObject[amount];
        for (int i = 0; i < coinToSpawn.Length; i  )
        {
            GameObject clone = Instantiate(coinPrefab, new Vector3(gameObject.transform.position.x, gameObject.transform.position.y, 0), Quaternion.identity);

            coinToSpawn[i] = clone;
        }

        for (int i = 0; i < itemsToSpawn.Length; i  )
        {
            GameObject clone = Instantiate(itemsToSpawn[i], new Vector3(gameObject.transform.position.x, gameObject.transform.position.y, 0), Quaternion.identity);

            itemsToSpawn[i] = clone;
        }
    }

}

They both have a structure like this: Empty GO, Sprite GO with animator Component and a box collider (isTrigger is off on this one) and another empty GO to detect the player trigger (this one isTrigger is on).

CodePudding user response:

Ok! I have an idea what might be causing problem but I am not sure.

Does big chest calling the small chest animation? If you are using same animator in both chests it will play the same animation.

Create a new animation for bigchest and drag&drop that one to the code.

And this explains why after you open small one bigchest code doesn't do anything. Because the "isOpen" is already equal to TRUE therefore cannot play the animation again.

CodePudding user response:

EDIT: I fixed with the help of a guru on the brackeys discord.

First I put a check on the trigger enter and exit to check if the player is in range.

And I did inheritance to the big chest, since both scripts have a lot in commom, and just overrited the lootdrop() method.

Btw, thanks to malibloo for the help to fix the issue. (:

  • Related