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 boddole · Feb 14, 2014 at 05:56 PM · c#initializationexecution order

Script Execution Order Question

Hello everyone, I've got a question about the execution of a script I made that deals with when values applied from the inspector are executed.

The problem: I made a script that requires that that bools be checked for the script to spawn certain prefabs in the scene. The script (in Awake) then adds prefabs based on the selections into a List. However, reguardless of the bool status, all prefabs are added into the list (and debug statements show that the script considers all bools to be true even if none actually are).

This is solved by moving the script into Start, but I was wondering what the logic behind the scenes was.

The script in question:

     void Awake ()
     {
         prefabNumberArray = new List<GameObject>();
         if (useRockPrefab1 == true)
         {
             Debug.Log ("adding rock1Prefab" + useRockPrefab1);
             prefabNumberArray.Add (rock1Prefab);
         }
 
         if (useRockPrefab2 == true)
         {
             Debug.Log ("adding rock2Prefab" + useRockPrefab2);
             prefabNumberArray.Add (rock2Prefab);
         }
 
         if (useRockPrefab3 == true)
         {
             Debug.Log ("adding rock3Prefab" + useRockPrefab3);
             prefabNumberArray.Add (rock3Prefab);
         }
 
         if (useRockPrefab4 == true)
         {
             Debug.Log ("adding rock4Prefab" + useRockPrefab4);
             prefabNumberArray.Add (rock4Prefab);
         }
     }
Comment
Add comment · Show 8
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 perchik · Feb 14, 2014 at 06:06 PM 1
Share

Interesting. I just made a script:

 public bool b1, b2, b3, b4;
     void Awake()
     {
         if (b1)
         {
             Debug.Log("1");
         }
         if (b2)
         {
             Debug.Log("2");
         } 
         if (b3)
         {
             Debug.Log("3");
         }
         if (b4)
         {
             Debug.Log("4");
         }
 
     }

If I check any of the 4 bools in the inspector, when I click play, it only prints out the number that was selected, which I believe is the intended behavior. This leads me to wonder, how do you declare your booleans?

avatar image Linus · Feb 14, 2014 at 06:09 PM 0
Share

This https://docs.unity3d.com/Documentation/$$anonymous$$anual/ExecutionOrder.html and this http://docs.unity3d.com/Documentation/Components/class-ScriptExecution.html should answer your question.

avatar image boddole · Feb 14, 2014 at 06:23 PM 0
Share

@perchik: below is the variable declarations that relate to the bools and list. Also, I noticed that in debug, the prefabs are being added: 3,4,1,2 for some reason.

 public class SpawnFloatingRocks : $$anonymous$$onoBehaviour {
     public bool useRockPrefab1;
     public bool useRockPrefab2;
     public bool useRockPrefab3;
     public bool useRockPrefab4;
 
     public GameObject rock1Prefab;
     public GameObject rock2Prefab;
     public GameObject rock3Prefab;
     public GameObject rock4Prefab;
 
     public List<GameObject> prefabNumberArray;
avatar image perchik · Feb 14, 2014 at 06:56 PM 0
Share

So, does this script work the "right way" for you?

test.cs:

 using UnityEngine;
 using System.Collections;
 
 public class test : $$anonymous$$onoBehaviour {
 
     public bool b1;
     public bool b2;
     public bool b3;
     public bool b4;
     void Awake()
     {
         if (b1)
         {
             Debug.Log("1");
         }
         if (b2)
         {
             Debug.Log("2");
         }
         if (b3)
         {
             Debug.Log("3");
         }
         if (b4)
         {
             Debug.Log("4");
         }
     }
 
 }
avatar image boddole · Feb 14, 2014 at 07:12 PM 0
Share

@perchik, yes, your script does work as anticipated.

Show more comments

1 Reply

· Add your reply
  • Sort: 
avatar image
-1

Answer by unimechanic · Feb 21, 2014 at 08:40 PM

Question solved according to comments, please summarize your solution as an answer for future reference. Adding this answer to remove it from the Unanswered view and give more relevance to other questions requiring attention. Thanks for your comprehension, Unity Support.

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

21 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

Related Questions

Initialising List array for use in a custom Editor 1 Answer

C# Non-Static Member Rigidbody2D.MovePosition 1 Answer

Problem with player movements 1 Answer

How to make specific text in a string array Bold C# 2 Answers

The name 'Joystick' does not denote a valid type ('not found') 2 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