- Home /
replacing object with another object at certain intervals
Let me try to explain my issue,
for the purpose of this question, I have placed multiple prefabs of 'grain sprouts' in my scene of various heights. Every 5 seconds I want to replace all the 'sprouts' objects with a larger grown plant and 5 seconds later with another prefab, till it is full grown at stage 4.
grain1 --> 5 secs --> grain2 --> 5 secs --> grain3 --> 5 secs --> grain4
This is what I have so far after days of searching and trying:
public class GrainGrow : MonoBehaviour {
private GameObject Grain1;
private GameObject Grain2;
private GameObject Grain3;
private GameObject Grain4;
private float time;
void Start() {
Grain1 = GameObject.FindGameObjectWithTag("Grain1");
Grain2 = GameObject.FindGameObjectWithTag("Grain2");
Grain3 = GameObject.FindGameObjectWithTag("Grain3");
Grain4 = GameObject.FindGameObjectWithTag("Grain4");
time = 0;
}
void Update ()
{
time += Time.deltaTime;
if (time >= 5) {
grow();
}
}
void grow(){
Instantiate(Grain4, Grain3.transform.position, Grain3.transform.rotation);
Instantiate(Grain3, Grain2.transform.position, Grain2.transform.rotation);
Instantiate(Grain2, Grain1.transform.position, Grain1.transform.rotation);
time = 0;
}
}
This will transform my last placed object to a higher level but not the others on the same level.
10 seconds after start, the new objects won't grow anymore.
note1: I dont just want to change the size of the object but replace it with a new one.
note2: I probably want to change the 'grow-time-interval' to a month, so when a new spout is planted 1 week later, it has to grow 1 week later as well.
I hope anyone can help me in the right direction with this. I am new to unity and c#. I am eager to learn but sometimes it is hard to find the right answer in the www-jungle
A Coroutine would be perfect for this, because inside you can wait for a specific peroid of time without blocking the rest of the program.
Example: https://docs.unity3d.com/560/Documentation/ScriptReference/WaitForSeconds.html
Answer by GarretLawrence · Jan 17, 2017 at 09:17 AM
Your problem is :
Say your Grain1 is at stage 1, after 5s you spawn a stage 2 grain right on Grain1's position, but this new spawned grain wont replace the Grain1.
So after 10s and later the script will keep spawning stage 2 grain at Grain1's position.
I have made a simple demo package what you exactly need.
I have problems with the download you gave me. Can you insert the script here in the comment?
I came up with this sollution, it works but I wonder if it is the best way to do it.
public class GrainGrow : $$anonymous$$onoBehaviour {
void Start() {
transform.GetChild (0).gameObject.SetActive (true);
transform.GetChild (1).gameObject.SetActive (false);
transform.GetChild (2).gameObject.SetActive (false);
transform.GetChild (3).gameObject.SetActive (false);
StartCoroutine (grow ());
}
IEnumerator grow(){
yield return new WaitForSeconds (5);
transform.GetChild (0).gameObject.SetActive (false);
transform.GetChild (1).gameObject.SetActive (true);
yield return new WaitForSeconds (5);
transform.GetChild (1).gameObject.SetActive (false);
transform.GetChild (2).gameObject.SetActive (true);
yield return new WaitForSeconds (5);
transform.GetChild (2).gameObject.SetActive (false);
transform.GetChild (3).gameObject.SetActive (true);
StopCoroutine (grow ());
}
}
https://drive.google.com/open?id=0B-IBnY$$anonymous$$2SOoJUGlub3FBbEdUZn$$anonymous$$
Try this. it is a full demo with scene. Its hard to understand how it work if i post only script.
Answer by Ulco · Jan 22, 2017 at 09:54 PM
Thank you Garret, that is a nice clean scene and it works exactly like I wanted.