• 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 _Toxic · Oct 15, 2015 at 11:01 AM · unity 5instantiate2d-platformerupdate functioncurrency

Instantiating an object only once within Update()

This is what i have so far. if i collect some coins they get stored in the GameBrain script. when i make it to a chest i can store the coins in there and that value gets stored in the GameBrain at coinsStored. i have that all working fine i even had the object instantiating after i stored 10 coins. its when i tried to make it happen only once by adding the if(UnlockComplete = false)statement. (i know there is nested loops i have fixed that but still no change.) that i have issues with it not spawning at all.

public class UnlockFloor : MonoBehaviour {

 public GameObject obj;                 //passed from unity
 public float UnlockAmout = 10f;         //usually set by unity 10 is default
 private bool UnlockComplete = false;    //Stops a MillionBajillion "Things" from spawning


 void OnDrawGizmosSelected() {            //A Gizmo so i can see the "Things" spawner
     Gizmos.color = new Color(1, 0, 0, 0.5F);
     Gizmos.DrawCube(transform.position, new Vector3(1, 1, 1));
 }
 

 void Update () {
     if(GameBrain.coinsStored > UnlockAmout) {
         if(UnlockComplete = false){
             UnlockTheThing();
             UnlockComplete = true;
         }
     } 
 }
 //Unlocks the "Thing" in question set by obj
 void UnlockTheThing(){
     Instantiate(obj, transform.position, 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 _Toxic · Oct 15, 2015 at 08:44 AM

For googlers coming here in the future 1st of all greetings from the past. 2nd here is what went wrong

if(UnlockComplete = false){

should read

if(UnlockComplete == false){

= needs to be ==

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 DDeathlonger · May 18, 2018 at 05:54 AM 0
Share

hahahahaha hello from the future! this was WAY different than I was looking for but I liked your greeting.. so ya know. XD

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.

Welcome to Unity Answers

The best place to ask and answer questions about development with Unity.

To help users navigate the site we have posted a site navigation guide.

If you are a new user to Unity Answers, check out our FAQ for more information.

Make sure to check out our Knowledge Base for commonly asked Unity questions.

If you are a moderator, see our Moderator Guidelines page.

We are making improvements to UA, see the list of changes.



Follow this Question

Answers Answers and Comments

4 People are following this question.

avatar image avatar image avatar image avatar image

Related Questions

How can I Instantiate two new GameObjects at the same time? (Attempting Asteroids style game) 2 Answers

Score is decrementing twice while passing through a collider. 1 Answer

Unity 2D addforce problem 1 Answer

Click and drag an object if holding a button down. 0 Answers

How do i Instantiate gameObjects in between multiple points? 2 Answers

  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges