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 /
This post has been wikified, any user with enough reputation can edit it.
avatar image
0
Question by luxidium · Dec 07, 2014 at 10:09 PM · instantiateupdatefunction

calling function once?

Under function update its checking to see If the score has changed, and if it has then it will call/trigger/activate the "Spawn" function and Instantiate an object. The problem is that it Instantiate hundreds of objects, its calling it every frame. how can I get to only call spawn once.

 #pragma strict
 
 var Score : float = 5f;
 
 
 var NPC : Transform;
 var NPCPoint1 : Transform;
 
 function Update(){
     if  (Score == 5){
         Spawn();
     }
 }
 
 function Spawn(){
         Instantiate(NPC, NPCPoint1.position, NPCPoint1.rotation);
 }





Comment
Add comment · Show 1
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 MrSoad · Dec 07, 2014 at 08:25 PM 0
Share

You start your Score variable at a value of 5. You never change this, so the condition in this line :

 if  (Score == 5){

is always true, so it will call your Spawn function every frame. A quick fix is to change the value of score inside this if statement :

 if  (Score == 5){
     Spawn();
     Score = 4f; //Or any other value that is not 5.
 }

3 Replies

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

Answer by ahmedbenlakhdhar · Dec 07, 2014 at 10:16 PM

You may add a boolean variable to check if the function was called, like this:

  #pragma strict
  
  var Score : float = 5f;
  
  var NPC : Transform;
  var NPCPoint1 : Transform;

  // This is the added boolean variable.
  // It initially contains false because Spawn() is not called yet
  var spawned : boolean = false;

  function Update(){
      // This condition does not get true value if spawned is true
      if  (Score == 5 && !spawned){
          Spawn();
          //Now spawned get true value because Spawn() is called
          spawned = true;
      }
  }

  function Spawn(){
       Instantiate(NPC, NPCPoint1.position, NPCPoint1.rotation);
  }
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 MrSoad · Dec 07, 2014 at 10:22 PM 1
Share

This is the better and more complete solution :)

avatar image
0

Answer by static_cast · Dec 07, 2014 at 10:25 PM

Instead of using booleans, you could implement two scores, with one that "catches up".

 var score = 5;
 var oldScore = 4;
 
 function Update() {
     if(score != oldScore)
         {
         Spawn();
         oldScore = score;
         }
 }

This way he could reset the trigger without relying on Spawn() triggering. ;)

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 MrSoad · Dec 07, 2014 at 10:28 PM 0
Share

This is not as good any of the previous solutions posted... You seem to like posting repeat answers to peoples questions, which serves no productive purpose :(

avatar image Alphapieter1 MrSoad · Feb 18, 2016 at 06:24 PM 0
Share

Dude shut up, his anwser was the best in this case it brought an simple solution to the problem and was reasonabel. probally the best answer the person will get. and where is he copying? i dont see any solution here that is nearly as short.

avatar image static_cast · Dec 07, 2014 at 10:34 PM 0
Share

Wow, mister. Hold your horses. First of all, your comment was the worst solution on here. Secondly, this is $$anonymous$$UCH $$anonymous$$UCH better than all of the other answers, as the guy said he was checking if the score changed, not if the score turned to five. $$anonymous$$aybe he wants multiples of five, but everybody else's scripts didn't show any way to reset.

avatar image MrSoad · Dec 07, 2014 at 10:36 PM 0
Share

$$anonymous$$ine was just a quick comment made before the question was published, and that is the point it is a comment, a push in the right direction, not an answer... You keep posting copycat answers on posts which is not a great thing to be doing!

avatar image static_cast · Dec 07, 2014 at 10:38 PM 0
Share

Apologies.

When a question gets posted on here, everybody goes to it and answers what they think is the solution. Obviously many of the answers are going to be similar, as it's the same problem.

Look at the other two answers on this question. They're both the same, but they have different variable names. At least $$anonymous$$e was different. :|

avatar image
0

Answer by Baste · Dec 07, 2014 at 10:22 PM

Just throw a trigger in there to turn off spawning once you've spawned one thing:

 var hasSpawned : bool = false;
 function Update(){
     if  (!hasSpawned && Score == 5){
         Spawn();
         hasSpawned = true;
     }
 }
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

7 People are following this question.

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

Related Questions

How to prevent object (bullet) from instantiating twice ? (It instantiates once for tapping the screen, and once more when beginning to hold touch on screen) ? 1 Answer

how to call something just once from an update function 5 Answers

Would like a function to change a parameter once, after an "if" statement has been met. 1 Answer

Calling a method once in update 1 Answer

How to detect empty slot and spawn gameobject there? 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