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 /
  • Help Room /
avatar image
0
Question by wallebot · Mar 16, 2016 at 12:51 AM · gameobjectstime.deltatimefunction callfunction update

Calling function every x seconds but objects disappearing instantly

function is being called properly however the objects only flash into view then disappear. There is no external source causing - it works well if a keystroke calls the function within update, but on any sort of time interval, whether Invoke() or otherwise - the objects only flash for an instant

 var timeBetweenTris:float = 0;
 
 function Start () {
 }
 
 function Update () {
 
     timeBetweenTris += Time.deltaTime;
     if(timeBetweenTris >= 5) {
         GenerateTri();
         timeBetweenTris = 0;
     }
 }
 
 function GenerateShape() {
     var randomNumber = Random.Range(1,9);
     if(randomNumber < 4) {
         return sphere;
     }
     else if(randomNumber < 7) {
         return capsule;
     }
     else {
         return cube;
     }
 }    
 
 function GenerateTri() {
     Instantiate(GenerateShape(), positionTop, Quaternion.identity);
     Instantiate(GenerateShape(), positionLeft, Quaternion.identity);
     Instantiate(GenerateShape(), positionRight, Quaternion.identity);
 }
 
Comment
Add comment
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

1 Reply

· Add your reply
  • Sort: 
avatar image
1
Best Answer

Answer by Ali-hatem · Mar 16, 2016 at 11:18 AM

  1. as i see you are trying to wait for some time before calling GenerateTri(); but it will work for the first time because Time.deltaTime start from 0.02 & will keep increasing so when Time.deltaTimegeting more than 5 then timeBetweenTris += Time.deltaTime;wil bee 6+0 or 7+0 so timeBetweenTris will bee always >5 .

  2. else if(randomNumber >3 && < 7)

  3. lastly if i understand you want to instantiate a 3 random objects after a delay of time you can use much easier way :

       public GameObject[] objs;//attach objects to this array in inspector.
          void Start () 
             {
                 StartCoroutine (GenerateTri());
             }
         IEnumerator GenerateTri()
         {
         Instantiate(objs[Random.Range (0, objs.GetLength (0))], positionTop, Quaternion.identity);
         Instantiate(objs[Random.Range (0, objs.GetLength (0))], positionLeft, Quaternion.identity);
         Instantiate(objs[Random.Range (0, objs.GetLength (0))], positionRight, Quaternion.identity);
         yield return new WaitForSeconds (5f);
         }
    
     
        
    
Comment
Add comment · Show 1 · 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 wallebot · Mar 16, 2016 at 07:16 PM 0
Share

EDIT:

Hi,

thank you so much for your answer. It turned out that the specific issue was related to a Destroy script which was not active - however was unaware that inactive scripts can still influence an object.

Your implementation amounts to one instantiation, however repeating generation was required.

Using an Array is much better thank you.

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

51 People are following this question.

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

Related Questions

Calling a function from another script? Not WORKING?! 2 Answers

Why is the speed of rb.velocity = new Vector2(speed * Time.deltaTime, 0) noticeably lower after game over than it is upon clicking play for the first time?,Issue with rb.velocity and time.deltatime 0 Answers

How do you access the Scripts, Functions, and Bools within an array of GameObjects? 0 Answers

A method running each millisecond in real time 0 Answers

Cannot convert 'UnityEngine.GameObject' to 'UnityEngine.Vector3' Why is this? 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