unity-game-enginegame-development

Why my asset keep disapearing once i press play?


I have an asset of a chest on my project. I copied to use it throughout the map with different items. But when I test the game, the copies are not where I put them. In fact it came to my attention, when I interact with the chest, it's like all the chests are in the same position.

At first, I tried spreading the assest in the map to see if it works. I revised the script and there is nothing wrong there. Seems to me it's something on Unity.

CHEST

    using System.Collections;
    using System.Collections.Generic;
    using UnityEngine;
    using EventArgs;
    using Item;

namespace Chest{

  public class ChestScript : MonoBehaviour {


    public Interaction interaction;
    public GameObject itemHolder;
    public Item.Item item;

    private Animator thisAnimator;

    private void Awake(){
      thisAnimator = GetComponent<Animator>();
    }

    private void Start() {
      interaction.OnInteraction += OnInteraction;
    }
    
    private void Update() {

    }

    private void OnInteraction(object sender, InteractionEventArgs args) {
      Debug.Log("Jogador acabou de interagir com o baú, que contem " + item.displayName);
      interaction.SetAvailable(false);
      thisAnimator.SetTrigger("tOpen"); 

      var itemObjectPrefab = item.objectPrefab;
      var position = itemHolder.transform.position;
      var rotation = itemObjectPrefab.transform.rotation;
      var itemObject = Instantiate(itemObjectPrefab, position, rotation);
      itemObject.transform.localScale = new Vector3(2, 2, 2);
      itemObject.transform.SetParent(itemHolder.transform);

      var itemType = item.itemType;
      if(itemType == ItemType.Key){
        GameManager.Instance.keys++;
      } else if(itemType == ItemType.BossKey){
        GameManager.Instance.hasBossKey = true;
      } else if(itemType == ItemType.Potion) {
        var player = GameManager.Instance.player;
        var playerLife = player.GetComponent<LifeScript>();
        playerLife.Heal();
      }
    }

    }
  }

INTERACTION

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

public class Interaction : MonoBehaviour {

    public GameObject widgetPrefab;
    [SerializeField] private Vector3 widgetOffset;
    public float radius = 10f;

    private GameObject widget;
    private bool isAvailable = true;
    private bool isActive;

    public event EventHandler<InteractionEventArgs> OnInteraction;

    private void OnEnable(){
        GameManager.Instance.interactionList.Add(this);

        }
    

    private void OnDisable(){
        GameManager.Instance.interactionList.Remove(this);
    }
 
    
    // Start is called before the first frame update
    void Start(){
        widget = Instantiate(widgetPrefab, transform.position + widgetOffset, widgetPrefab.transform.rotation);
        widget.transform.SetParent(gameObject.transform, true);
        
        var worldUiCamera = GameManager.Instance.worldUiCamera;
        var canvas = widget.GetComponent<Canvas>();
        if(canvas != null){
            canvas.worldCamera = worldUiCamera;
        }
        
        var interactionWidgetComponent = widget.GetComponent<InteractionWidget>();
        if(interactionWidgetComponent != null){
            interactionWidgetComponent.worldUiCamera = worldUiCamera;
        }
    }

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

    public bool IsActive(){
        return isActive;
    }

    public void SetActive(bool isActive){
        var wasActive = this.isActive;
        this.isActive = isActive;

        var interactionWidget = widget.GetComponent<InteractionWidget>();
        if(isActive){
            interactionWidget.Show();
        } else {
            interactionWidget.Hide();
        }

    }

    public bool IsAvailable(){
        return isAvailable;
    }

    public void SetAvailable(bool isAvailable){
        this.isAvailable = isAvailable;
    }

    public void Interact(){
        OnInteraction?.Invoke(this, new InteractionEventArgs());
        

    }
}

The chest on the right is the original


Solution

  • In the Animation track used you need to set the Track offset to Apply Scene Offsets