The object is lost when picking up an object from the ground. I want him to come back after a while.
I am using a bag system when i click on the object on the ground the object (setactive = false) I want it to come back in the same place after a while.
Like picking up food from the ground in games.
I tried this with elapsed but failed.
Here is my code that doesn’t work
public class ItemPickUp : MonoBehaviour
public Item Item;
public GameObject gameobj;
private void OnMouseDown()
elapsed += Time.deltaTime;
if (elapsed >= 2f)
elapsed = elapsed % 1f;
This line is not checking if gameobj is active or not.
Be careful for what methods return, and what does that return value means.
From the documentation:
public void SetActive(bool value);
SetActive is a void method, it means it returns nothing for you to read.
SetActive is the method you call to set your object active or not.
To check if a gameobject is active, use instead
gameobj.activeInHierarchy which returns a bool true or false, based on the state of the object in hierarchy.
In your case, you can replace the line of code
if(gameobj.activeInHierarchy == false) to check if gameobj is not active.
I did it as you said, the game did not give an error, but still what I tried to do does not work.
Can this be done any other way?