I have a treasure chest that is a prefab and when I open one chest in the scene, the other chest opens as well. I’m looking for the two chests to not interact with each other even though it is the same prefab.
I can make what I’m looking to do work if I create an individual script for each treasure chest, but I’m looking to have maybe a hundred chests in the game, so that is not very efficient.
Ultimately, I’m seeing if there is a way to code a prefab so that is it does not interact with others of the same prefab. Is that something that is possible?
I’m not great at coding, so I would not be surprised if this is an inefficient/horrible way of coding. The bold part is what I’m currently using for PlayerPrefs to save between scenes… The gist of this is if I have a key, the chest highlights green and can be opened, if I don’t have a key, it highlights red and can’t be opened. Once opened, the treasure appears after 2 seconds. Thanks for any help!!
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class TreasureChestMid : MonoBehaviour
{
private Animator myAnimator;
public bool openTCM = false;
public bool tCMTreasure;
private BoxCollider2D treasureChestMidBoxCollider;
public GameObject tcmHighlight;
public GameObject tcmHighlightlocked;
private GameManager gm;
AudioManager am;
public string chestopen;
public string treasure;
public string keychestunlock;
void Start()
{
myAnimator = GetComponent<Animator>();
treasureChestMidBoxCollider = GetComponent<BoxCollider2D>();
gm = GameObject.FindGameObjectWithTag("GM").GetComponent<GameManager>();
am = AudioManager.instance;
}
void OnTriggerEnter2D(Collider2D col)
{
if (col.gameObject.tag == "Player")
{
if (gm.tKeys >= 1)
{
openTCM = true;
tcmHighlight.SetActive(true);
}
else
{
tcmHighlightlocked.SetActive(true);
}
}
}
void OnTriggerExit2D(Collider2D col)
{
if (col.gameObject.tag == "Player")
{
if (gm.tKeys >= 1)
{
openTCM = false;
tcmHighlight.SetActive(false);
}
else
{
tcmHighlightlocked.SetActive(false);
}
}
}
void Update()
{
if (treasureChestMidBoxCollider == null)
{
openTCM = false;
}
if (openTCM == true && Input.GetKeyDown(KeyCode.Q))
{
Destroy(treasureChestMidBoxCollider, 1f);
tcmHighlight.SetActive(false);
tcmHighlightlocked.SetActive(false);
gm.tKeys -= 1;
am.PlaySound(keychestunlock);
StartCoroutine(treasureUnlock());
StartCoroutine(treasureDelay());
StartCoroutine(treasureGet());
}
***if (gm.tCMStayOpen == true)
{
myAnimator.SetTrigger("IsTreasureChest(Mid)");
tcmHighlight.SetActive(false);
tcmHighlightlocked.SetActive(false);
}***
}
IEnumerator treasureUnlock()
{
yield return new WaitForSeconds(1);
myAnimator.SetTrigger("IsTreasureChest(Mid)");
am.PlaySound(chestopen);
}
IEnumerator treasureDelay()
{
yield return new WaitForSeconds(2);
tCMTreasure = true;
gm.tCMStayOpen = true;
}
IEnumerator treasureGet()
{
yield return new WaitForSeconds(2);
am.PlaySound(treasure);
}
}
I was browsing through and I saw this question and I immediatly tought about Prefab Variants.
I guess if you create one prefab variant for each chest, the effect you want will be achieved.
You could also fix this through code but give my answer a try and post some feedback after
You say that if (gm.tCMStayOpen == true) is stored in PlayerPrefs. That means that every chest in the game will look at this same bool and will open itself. Maybe you want to move this bool into the chest script itself, instead of GameManager, so each chest has its own tCMStayOpen.