Hey! I have a question and wonder how do I make the trees respawn after 5-7 minutes after I’ve chop them down?
look up invoke
when the tree Is chopped down, run an invoke with a time of 5 mins (600 sec)
lookup Instantiate to respawn the tree game object prefab
@johne5
Can you help me script it?
using UnityEngine;
using System.Collections;
public class InvokeScript : MonoBehaviour
{
public GameObject target;
void Start()
{
Invoke (“SpawnObject”, 600);
}
void SpawnObject()
{
Instantiate(target, new Vector3(96.392, -8.7493, 0.70139), Quaternion.identity);
}
}
You could also consider DateTime and TimeSpan for this and there’s really no need to despawn / destroy the chopped down trees. You could simply change/animate the model or the sprite of the tree to chopped down version. Alternatively you could just hide the model by deactivating it and it’s colliders.
if you want to grow it elsewhere just assign it a new position on Regrow.
Don’t deactivate (or destroy) the root gameobject with the script object because that will stop the co-routine. Alternatively you can make script that finds all trees on awake and calls CheckGrowth on all cut down trees.
using UnityEngine;
using UnityEngine;
using UnityEngine.EventSystems;
using System;
using System.Collections;
public class TreeBehavior : MonoBehaviour, IPointerClickHandler
{
public float secondsToRegrow = 300;
private bool canHarvest = true;
private DateTime harvestTime;
void CutDown()
{
if(canHarvest)
{
harvestTime = DateTime.UtcNow;
canHarvest = false;
StartCoroutine(CheckGrowth());
//just an example, instead the change model, sprite or animation to cut down
transform.localScale = Vector3.one * 0.5f;
}
}
void Regrow()
{
canHarvest = true;
//just an example, instead the change model, sprite or animation to fully grown
transform.localScale = Vector3.one;
}
IEnumerator CheckGrowth()
{
while(!canHarvest)
{
yield return new WaitForSeconds(1.0f);
TimeSpan span = DateTime.UtcNow - harvestTime;
if (span > TimeSpan.FromSeconds(secondsToRegrow))
Regrow();
}
}
public void OnPointerClick(PointerEventData eventData)
{
CutDown();
}
}
Thanks! Do i just attach this script to my tree and then it will respawn? @Vipsu
hey Vipsu, that script looks pretty good.
@jontebeatz1337 Here is what I would do. I’ve attached my unity files for you to look at and import.
You’ll need to create an empty gameobject to hold the script. you’ll need to create a tag name “tree”. and assign the tag to a tree prefab. drag the prefab and camera to the script. and you should be good to go.
you seem new to programming so I didn’t want to through to many things at you all at once. you could easily add to this script, let say you wanted to force the player to click 3 times before the tree is chopped down, you should do that
what my script does.
you use the empty gameobject as the spawn point. so place a few in the scene. when the script starts, it spawns a tree as a child object. the update is waiting for the mouse to be clicked. when the mouse is click we shoot a raycast and check if we hit anything. if we hit something, check it’s tag name. we are checking for the tag name “tree”. if we hit a tree then remove all child gameobjects and set the invoke time to 600 sec. and start the process over!
my code
using UnityEngine;
using UnityEngine.EventSystems;
using System;
using System.Collections.Generic;
public class InvokeScript : MonoBehaviour
{
public GameObject treePrefab;
public float regrowTime = 600f;
private bool canChopDown = true;
public Camera myCamera;
void Start()
{
Invoke("SpawnObject", 0);
}
private void Update()
{
if (Input.GetMouseButtonDown(0))
{
RaycastHit hit;
Ray ray = myCamera.ScreenPointToRay(Input.mousePosition);
if (Physics.Raycast(ray, out hit))
{
if (hit.transform.tag == "tree")
{
Debug.Log("You Clicked on a Tree");
CutDown();
}
}
}
}
void SpawnObject()
{
var root = Instantiate(treePrefab, new Vector3(0f, 0f, 0f), Quaternion.identity) as GameObject;
root.transform.parent = gameObject.transform;
canChopDown = true;
}
void CutDown()
{
if (canChopDown)
{
canChopDown = false;
var children = new List<GameObject>();
foreach (Transform child in transform) children.Add(child.gameObject);
children.ForEach(child => Destroy(child));
Debug.Log("Spawn a new tree in " + regrowTime + " sec");
Invoke("SpawnObject", regrowTime);
}
}
}
3099521–233981–ChopTree.unitypackage (9.43 KB)
Thanks for that! But I do not know if I’m doing something wrong but it does not seem to work for me? I did as you said I would do. I added the tree tagg “tree” to the tree and added the script. I attach the tree prefab to the script and the cameran and then I made empty gameobject and added the tag “spawn point” but is that something I’ve missed? @johne5
The script does not go on the tree prefab. it goes on the empty gameobject (AKA the spawn point). from the script that’s in the empty gameobject you need to attach the camera and tree prefab. The spawn point does not need a tag for my script to work.
you need to verify that the tree prefab has a collider on it.
Thanks!
did you get it working?
Hi i thought i would give it a go , the only problem i have is the trees spawn at the gui camera everytime and not at the camera i pick
It sounds like you didn’t create empty gameobjects as the spawn point for the tree.
Not sure what you mean by the camera you pick
FP Camera and world camera , i will try creating another empty gameobject as the spawn point