Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by Ulco · Jan 17, 2017 at 08:21 AM · timeobjectsreplaceinterval

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

Comment
Add comment · Show 2
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image doublemax · Jan 17, 2017 at 08:26 AM 0
Share

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

avatar image Ulco · Jan 21, 2017 at 10:56 AM 0
Share

thanks, that works

2 Replies

· Add your reply
  • Sort: 
avatar image
0

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.

Download link

Comment
Add comment · Show 3 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Ulco · Jan 21, 2017 at 02:37 PM 0
Share

I have problems with the download you gave me. Can you insert the script here in the comment?

avatar image Ulco Ulco · Jan 21, 2017 at 11:01 AM 0
Share

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 ());
     
     }
 
 
 
 }
avatar image GarretLawrence Ulco · Jan 22, 2017 at 10:08 AM 0
Share

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.

avatar image
0

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.

Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

6 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

How to Have a Random Interval in an Array in between Sounds being Played? 1 Answer

Handling two triggering objects at the same time 0 Answers

Gradually scale platform? 4 Answers

i want tospawn objects 1 Answer

SOLVED - String replace % with " in C# 1 Answer


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges