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
0
Question by moghes · May 30, 2012 at 02:26 PM · updatefunctionstartboo

bool value does not change

Hello everyone! I have a simple code here that's causing me problems. I want to create my prefab once, so that I have my boolean here to check, I can't put my code in a Start function because I might call a function CreateNext() to change the bool value to true, so that the update funciton waits until I tell(call function CreateNext) it to create an another prefab.

 private var create = true;
 
 function Update()
 {
   if(create)
    {
      var prefab:GameObject =    Instantiate(myPrefab,gameObject.transform.position,Quaternion.identity);
     prefab.transform.parent = transform;
     create = false;
    }    
 }
 
 public function CreateNext()
 {
    create = true;
 }

but as i run the game, a prefab is created non-stop that means the boolean setting to false didn't work. any ideas where's might be my mistake?

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 save · May 30, 2012 at 02:33 PM 0
Share

I'd suggest you have all the instantiation code in CreateNext(), then you don't have to use a boolean.

1 Reply

· Add your reply
  • Sort: 
avatar image
0

Answer by DocteurCox · May 30, 2012 at 03:03 PM

Wouldn't it be easier to use the Singleton pattern ?

Instead of having a boolean that defines whether or not your prefabs has been created, put the instance of your prefab in a static variables. Then to know if your prefab has already been created, just check if your static variable already exist. For example :

 private static Instance = null;
 
 void Update()
 {
     if(Instance == null)
     {
         // Create instance of your object here
     }
 }

That method works with C#, don't know if it works with other language.

Hoped it helped ;)

Comment
Add comment · Show 2 · 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 moghes · May 30, 2012 at 03:07 PM 0
Share

Thanks, But I need to control that! as you see in my code, I have a public function CreateNext which can change the value of the boolean. So I need it to be created once as the game starts, but not once for the whole game! thx btw

avatar image DocteurCox · May 30, 2012 at 03:14 PM 1
Share

Well, this isn't completely incompatible with my method. Let's assume you want to switch to next prefab like you would have done with CreateNext, you would just have to set Instance to null. :)

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

5 People are following this question.

avatar image avatar image avatar image avatar image avatar image

Related Questions

Start, Awake, Update. Any other ways to call functions from an empty GameObject? 3 Answers

Detecting Input in the Start function (C#) 1 Answer

Script initialization when instancing a game object 0 Answers

Rigidbody.AddForceAtPosition & functions 1 Answer

Im a bit confused on a simple script 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