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 user-12236 (google) · May 01, 2011 at 09:28 AM · compiler

Unity being selective about which commands to execute.

This is a very annoying problem that I have had far more times than I should. I guess it's because I'm an old school programmer, and I'm used to command lines being executed from first to last, but it seems that unity likes to pick and choose which command lines it feels like executing. As far as I can tell, unity seems to run the scripts "sideways", meaning that each line in the script is run simultaneously. In this particular case, it simply refuses to toggle the booleans "amDone" and "move". I've tried moving them around in the script and even moving the toggle operations into their own function, all with the same result. They will not toggle. Here is the whole script.


var rotSpeed : float; var walkSpeed : float; var buildPoint : Transform; private var minTargX : float; private var minTargZ : float; private var maxTargX : float; private var maxTargZ : float; var move : boolean; private var myX : float; private var myY : float; private var myZ : float; var house : Transform; var amDone : boolean = false; var townCenter : Transform; //-------------------------------------

     function Update(){


     myX = transform.position.x;
     myY = transform.position.y;
     myZ = transform.position.z;


     if (buildPoint) {

         minTargX = (buildPoint.transform.position.x - 1);
         minTargZ = (buildPoint.transform.position.z - 1);
         maxTargX = (buildPoint.transform.position.x + 1);
         maxTargZ = (buildPoint.transform.position.z + 1);


     if (myX > minTargX && myX <= maxTargX && myZ > minTargZ && myZ <= maxTargZ){
         move = false;}

     else{ 
         move = true;}


     if (move == true){
         var rotation = Quaternion.LookRotation(buildPoint.position - transform.position);
     transform.rotation = Quaternion.Slerp(transform.rotation, rotation, Time.deltaTime * rotSpeed);
          transform.Translate(0, 0, Time.deltaTime * walkSpeed);
         }

     else {
         if (amDone == false){

         BuildAHouse();
         TownCenter.housesBuilt ++;
     }
     }

     }}

     //------------------------------------------------

     function BuildAHouse(){


        transform.LookAt(townCenter);
        houseIBuilt = Instantiate(house, Vector3(myX,2.9,myZ), transform.rotation);
         transform.parent = houseIBuilt;
     transform.localPosition = Vector3(0, -2, 6);
     transform.parent = null;
     DidIt();
     }

     function DidIt(){
          amDone = true;
           move = false;

         }


So, by all logic, the builder should stop seeking the build point after building the house and teleporting to the outside, yet amDone and move will not toggle, causing the builder to simply run straight into the wall of the house, instead of waiting for the next command. Any insight on this will be much appreciated, as this is a very frustrating issue. These are simple commands, and there is absolutely no reason why these two lines of code should not execute. Sorry for the rant.

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

2 Replies

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

Answer by Eric5h5 · May 01, 2011 at 09:36 AM

I guess it's because I'm an old school programmer, and I'm used to command lines being executed from first to last, but it seems that unity likes to pick and choose which command lines it feels like executing.

This never happens. If lines aren't executing, it's 100% guaranteed that you made a logic error.

As far as I can tell, unity seems to run the scripts "sideways", meaning that each line in the script is run simultaneously.

No, that's physically impossible; a CPU can't do that. (With multiple cores, yes, but that's a different issue and doesn't apply here; everything in Unity code is one thread unless you explicitly use System.Threading.)

I haven't really looked at your code closely; it's very difficult to tell what's happening with formatting like that. Clean it up and you may see where the problem is. Also, it looks like you're scheduling events, in which case it would be simpler to use coroutines instead of a ton of if/then stuff in Update.

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 user-12236 (google) · May 01, 2011 at 09:50 AM 0
Share

heh. you have a point. sorry about the atrocious formatting. it seems I did indeed make a logic error. the lines were executing, but the variables were being set back to their original states during update because the builder was outside of the detection range for the build point. I ended up settling for increasing the detection range.

avatar image
0

Answer by user-12236 (google) · May 01, 2011 at 09:39 AM

Alright, for the benefit of any unfortunate souls out there who have this same issue, I have found two solutions.

First, which is highly undesirable because of the way I have the game set up, I can end the script with this line.

buildPoint = null;

The other solution is to increase the range of detection by changing this

            minTargX = (buildPoint.transform.position.x - 1);
            minTargZ = (buildPoint.transform.position.z - 1);
            maxTargX = (buildPoint.transform.position.x + 1);
            maxTargZ = (buildPoint.transform.position.z + 1);

to this

    minTargX = (buildPoint.transform.position.x - 3);
    minTargZ = (buildPoint.transform.position.z - 3);
    maxTargX = (buildPoint.transform.position.x + 3);
    maxTargZ = (buildPoint.transform.position.z + 3);

which makes no sense to me, but ok, wtf ever.

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

No one has followed this question yet.

Related Questions

The name 'Joystick' does not denote a valid type ('not found') 2 Answers

In-Game scripting works in Editor (Try it!) - But Not Build 3 Answers

Scripting in inspector, possible? 1 Answer

Project scripts stop working after building it in Android 1 Answer

How to import the object from server to unity 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