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 Magi4ever · Aug 25, 2016 at 11:37 AM · javascriptinstantiatedestroy

Create only one Instantiated Object

Okay guys - I can't figure this one out. I have a day night cycle going by the rotation of the directional light, and at night I need a fire particle system prefab I have to start up, and during the day I need it to go away. How I have it set up right now, it creates to many instances and possibly would kill the game object rather than the instance? I need this Day- No fire/ Night - Fire cycle to continue for an infinite amount of time. Here is the scripts I have

This one is for the rotation of the light and to tell something that the rotation is correct for Day time or Night time

 #pragma strict
 
 public static var TimeOfDay : float = 0.0; 
 public static var LightRotation : float = 0.0;
 
 function Update()
     {
     transform.Rotate(Vector3.right / 3.0);
     LightRotation = transform.eulerAngles.x; 
 
     if (LightRotation <= 150) {
         TimeOfDay = 0.0;
         BroadcastMessage("KillFire");
         Debug.Log(TimeOfDay);
     }
     else if (LightRotation >= 150) {
         TimeOfDay = 1.0;
         BroadcastMessage("StartFire");
         Debug.Log(TimeOfDay);
     }
 }
 
 This is what I have on the empty object to create the fire instance

 #pragma strict
 
 public var prefab : GameObject;;
 
 function KillFire(){
     Destroy(prefab);
 }
 
 function StartFire(){
     Instantiate(prefab,transform.position, Quaternion.identity);
 }
 
       

Any and all help would be greatly appreciated. Thank you!

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
0

Answer by DiegoSLTS · Aug 25, 2016 at 12:34 PM

First, you're destroying the prefab, not the instance, that KillFire won't work as expected. You have to keep a referente to the instance of the fire and destroy that reference.

That also works for your actual question. You're calling StartFire every frame if LightRotation is greater than 150, so you're creating an instance every frame. Keeping a reference to the instance you can check if it's null before doing the Instantiate code.

Anyway, a cleaner approach would be to only broadcast the messages if LightRotation passed 150 in that same frame. SendMessage and BroadcastMessage are expensive and shouldn't be called if will be ignored by the receiver.

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

4 People are following this question.

avatar image avatar image avatar image avatar image

Related Questions

Performance of instantiate and destroy for bullets 2 Answers

Cannot destroy the SphereCollider on Instantiated Objects 1 Answer

I want to Instantiate a force field - let it work for x(sec) show countdown and then make wait for a count up - but then my button doesn't seem to work anymore? 0 Answers

I need help with this script. Can't figure out my it isn't working.,Can't get this script to work 0 Answers

Trying to replace 2 objects. Destroying assets is not permitted to avoid data loss. 0 Answers


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