Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 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
1
Question by Trr1ppy · Dec 25, 2013 at 03:45 PM · c#renderer.enabled

Requesting help with Time.time & Instantiate script.

Greetings

So I want to instantiate an object after a certain time (4 seconds) and the code runs fine, no errors appear and I can execute the game but no object instantiates after the 4 seconds? Here's my code:

using UnityEngine; using System.Collections; public class Player : MonoBehaviour { public GameObject EnemyFab; float gameClock; void Update () { gameClock = Time.time; if (gameClock == 4.0f) { Debug.Log("yo"); Vector3 position = new Vector3(Random.Range(-5, 5), 11, 0); Instantiate(EnemyFab, position, Quaternion.identity); } } }

I know there is a couple of unnecessary things in there but I've been trying everything I could think of to make the Enemy Instantiate. By the way I am not getting any yo's in the debug. I'd be grateful for any help or suggestions!

EDIT: Also I had another question; So instead of destroying my object after it collides with an enemy I set renderer.enabled = false; and then a couple of seconds later it reapers when I set renderer.enabled = true; but all that time is isn't rendered its still physically has a collier and a transform and can still collide with objects. So I would prefer a ?Function?Code?Method?(don't even know what to call it :| ) instead of renderer.enabled that will physically move it or disable it for a certain amount of time?

Thank you very much for your time :)

Comment
Add comment · Show 6
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 highpockets · Dec 25, 2013 at 04:10 PM 0
Share

$$anonymous$$aybe try gameClock >= 4.0f.... The time might not become exactly 4.0

avatar image Trr1ppy · Dec 25, 2013 at 04:17 PM 0
Share

Already tried that and after the first 4 seconds of the game its spawned a new enemy on the screen for every frame update. Vice versa does the same just for the first 4 seconds of the game. Thanks for the help :)

avatar image highpockets · Dec 25, 2013 at 04:26 PM 0
Share

You could have

bool enemySpawned = false;

if(gameClock >= 4.0f && !enemySpawned) {

enemySpawned = true; //instantiation code.... }

$$anonymous$$erry xmas

avatar image ozturkcompany · Dec 25, 2013 at 04:55 PM 0
Share

you cannot compare two floating points.Use $$anonymous$$athf Approx or use > or < ins$$anonymous$$d of testing for equal results.

avatar image Trr1ppy · Dec 26, 2013 at 03:03 PM 0
Share

$$anonymous$$athf.Approximately http://docs.unity3d.com/Documentation/ScriptReference/$$anonymous$$athf.Approximately.html

Show more comments

1 Reply

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

Answer by Tomer-Barkan · Dec 25, 2013 at 04:54 PM

The update code runs every frame. The time between frames varies depending on the performance of your code, graphics, PC speed, etc (what we call framerate is how many frames are run per second). So you don't know exactly how long between frames.

The chances that an update will run in exactly 4.0 seconds is small. You'll need to check it differently, you need to check if at least 4 seconds have passed.

Here's the "Unity" way of doing it:

 public GameObject EnemyFab;
 float spawnTime = 4;
  
 void Update () 
 {
     if (spawnTime >= 0 && Time.time >= spawnTime)
     {
         Debug.Log("yo");
         Vector3 position = new Vector3(Random.Range(-5, 5), 11, 0);
         Instantiate(EnemyFab, position, Quaternion.identity);
         spawnTime = -1;
     }
 }

 public void SpawnInSeconds(float seconds) {
     spawnTime = Time.time + seconds;
 }

So basically what we're doing is keeping track of when the enemy should spawn, and putting it in spawnTime. It starts with 4, so initially the enemy will be spawned after 4 seconds. Then every frame we checked whether the time to spawn has already passed, and if it has we will spawn the enemy, and set the time to spawn to a negative number (meaning it will never spawn until we change it). If you want to spawn in X seconds, simply call SpawnInSeconds(X), and it will set spawnTime to be X seconds from now.

About your second question, you can deactivate it with:

 enemy.gameObject.SetActive(false);

and then enable it again with

 enemy.gameObject.SetActive(true);
Comment
Add comment · Show 5 · 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 Trr1ppy · Dec 26, 2013 at 04:00 AM 0
Share

Thankyou very much! I understand how its unlikely for Time.time to exactly equal 4, how we check if 4 seconds has passed and how essentially we switch it off but defining spawnTime as a number below zero (-1). I am very grateful as it works perfectly. But I have never dealt with 'calling' a function, I understand how the code works inside the function but I don't know how to call it.

And I tried Replacing renderer.enabled with gameObject.SetActive and it will remove when set to false so the player object cannot collide or interact or even move but it isn't being set back to true. Its weird because I put back in the renderer.enabled to check if the If argument is at fault and it worked as it has been before. So the problem must lie with the gameObject.SetActive. Here's my code: float timer = 0f; void Update () { if (Time.time - timer > 2) { gameObject.SetActive(true); } } void OnTriggerEnter(Collider collider) { if(collider.gameObject.CompareTag("Astroids") && Time.time - timer > 4) { playerLives -= 1; gameObject.SetActive(false); timer = Time.time; if (playerLives < 1) { Destroy(this.gameObject); } } if (collider.gameObject.CompareTag("Boss") && Time.time - timer > 4) { playerLives -= 1; gameObject.SetActive(false); timer = Time.time; if(playerLives < 1) { Destroy(this.gameObject); } } } } Thanks for your help!

avatar image Tomer-Barkan · Dec 26, 2013 at 06:41 AM 0
Share

Calling a function is what you do when you set the gameObject to be active for example. If you'd want to call my function, simply use SpawnInSeconds(5); at any part of your code for example. It's just a regular C# class function, method, or whatever you call it. Nothing special about it.

Disabling a game object using gameObject.SetActive(), will do exactly that - disable the gameobject. That also means that the scripts of this gameobject will no longer execute, hence you cannot enable it from within it's own update code (that code is not running!)

What you could do is have an object that is always active, and it will take care of enabling and disabling other objects using coroutines:

 public class Disabler : $$anonymous$$onoBehaviour {
     public void Disable$$anonymous$$e(GameObject me, float time) {
         StartCoroutine(Dsb(me, time));
     }
 
     public IEnumerator Dsb(GameObject obj, float time) {
             Debug.Log("Deactivating");
             obj.SetActive(false);
             yield return new WaitForSeconds(time);
             Debug.Log("Activating");
             obj.SetActive(true);
     }
 }

Now all you need is to set a reference from your class to the object that has the disabler script, and call it when you want to disable your object:

 public Disabler disabler;

 void OnTriggerEnter(Collider collider)
 {
     if(collider.gameObject.CompareTag("Astroids") && Time.time - timer > 4)
     {
         playerLives -= 1;
         timer = Time.time;
  
         if (playerLives < 1)
         {
             Destroy(this.gameObject);
         }

         disabler.Disable$$anonymous$$e(gameObject, 2);
     }
  
     if (collider.gameObject.CompareTag("Boss") && Time.time - timer > 4)
     {
         playerLives -= 1;
         timer = Time.time;
  
         if(playerLives < 1)
         {
             Destroy(this.gameObject);
         }
         disabler.Disable$$anonymous$$e(gameObject, 2);
     }
 }

Don't forget to set the reference to the disabler object from the inspector.

avatar image Trr1ppy · Dec 26, 2013 at 09:00 AM 0
Share

Ahh I understand what calling a function is, silly me! Yes I see how the game object and therefore the script doesn't exist after gameObject.SetActive(false);.

I'm afraid all those coroutines is over my head and out of my scope for my first attempt at scripting an entire project, and really what I've got render.enabled works fine for what I need just has the side affect of still colliding with objects. So I think I'm gonna stick to the rule: If it isn't broken don't fix it.

Thank you very much for your help! much appreciated!

avatar image Tomer-Barkan · Dec 26, 2013 at 11:54 AM 0
Share

If you want you can also disable the collider and prevent it from colliding:

 collider.enabled = false; // true to enable

I linked the coroutine tutorial, it could be useful later. Basically it's a method that can "yield" control, and then automatically resume after a couple of seconds (there are other uses as well).

avatar image Trr1ppy · Dec 26, 2013 at 02:38 PM 0
Share

collider.enabled will work well with render.enabled because I've decided that players still need the visual cue that they've lost a life.

Since this project is nearly wrapped up I think in my next project I'll learn to use coroutines because they seem very useful! Thanks for the link!

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

22 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

Related Questions

Multiple Cars not working 1 Answer

Distribute terrain in zones 3 Answers

Flip over an object (smooth transition) 3 Answers

How to whrite in c# the code: onMouseEnter Hide mesh 1 Answer

Making a bubble level (not a game but work tool) 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