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 Slipperz · Oct 21, 2013 at 11:24 PM · booleansimpleformat

Simple Boolean Logic/Formatting Question

This has to be something simple, but I am new to Java and still learning, I've made it this far without having to ask a personal question but it's got me stumped.

I have three boxes, when they collide with my bullet the boolean should switch to false, when all three boxes have switched their assigned boolean to false the player should be taken to the game over screen. With Debug.Log I am able to see it's reading them as false, but it isn't switching them over in the inspector. So I feel like I might be instantly setting it back somehow. Here is all of my code for this particular script.

Edit:I should have clarified, updating the original post. It is attached to the prefab that is my bullet. So, all of these happen when my bullet experiences a collision. There is only one script.

 static var Counter : int = 0;
 var object : GameObject;
 var success : AudioClip;
 var thud : AudioClip;
 var explosion : GameObject;
 var fail : AudioClip;
 var boxhit1 : boolean = true;
 var boxhit2 : boolean = true;
 var boxhit3 : boolean = true; 
 
     
 function OnCollisionEnter (theCollision : Collision){
 
     if(theCollision.rigidbody){
     Instantiate(explosion, transform.position, transform.rotation);
     AudioSource.PlayClipAtPoint(fail, Camera.main.transform.position);
     Destroy(object);
     }
     
     else if(theCollision.gameObject.name == ("Box1") && boxhit1){
     theCollision.gameObject.AddComponent(Rigidbody);
     gameObject.Find("Box1").renderer.material.color = Color.black;
     Instantiate(explosion, transform.position, transform.rotation);
     AudioSource.PlayClipAtPoint(success, Camera.main.transform.position);
     Counter++;
     boxhit1=!boxhit1;
     Debug.Log("Box Hit 1: " + boxhit1);
     Destroy(object);
     }
     
     else if(theCollision.gameObject.name == ("Box2") && boxhit2){
     theCollision.gameObject.AddComponent(Rigidbody);
     gameObject.Find("Box2").renderer.material.color = Color.black;
     Instantiate(explosion, transform.position, transform.rotation);
     AudioSource.PlayClipAtPoint(success, Camera.main.transform.position);
     Counter++;
     boxhit2 = false;
     Debug.Log("Box Hit 2: " + boxhit2);
     Destroy(object);
     }
     
     else if(theCollision.gameObject.name == ("Box3") && boxhit3){
     theCollision.gameObject.AddComponent(Rigidbody);
     gameObject.Find("Box3").renderer.material.color = Color.black;
     Instantiate(explosion, transform.position, transform.rotation);
     AudioSource.PlayClipAtPoint(success, Camera.main.transform.position);
     Counter++;
     boxhit3 = false;
     Debug.Log("Box Hit 3: " + boxhit3);
     Destroy(object);
     }
     
     else if(theCollision.gameObject){
     Instantiate(explosion, transform.position, transform.rotation);
     AudioSource.PlayClipAtPoint(thud, transform.position);
     Destroy(object);
     }
     }
 
 function Update(){
 
 if(!boxhit1 && !boxhit2 && !boxhit3){
     Debug.Log("All are false!");
     Application.LoadLevel("GameOver"); 
     }
 }
     
Comment
Add comment · Show 5
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 Huacanacha · Oct 21, 2013 at 11:28 PM 0
Share

That's not Java, it's Unity Script. And Unity Script is not Javascript, although it's syntactially nearly identical. You can thank Unity, and the people who changed LiveScript to Javascript for the confusion.

You should know this before you try debugging ;)

avatar image Slipperz · Oct 21, 2013 at 11:33 PM 0
Share

That is definitely ter$$anonymous$$ology I'll keep in $$anonymous$$d, I wasn't sure how to properly describe it,

avatar image rutter · Oct 21, 2013 at 11:46 PM 0
Share

What's this script attached to? In particular, is there more than one of it? I ask because each instance of your script is going to have its own copies of boxhit1, boxhit2, and boxhit3.

avatar image Slipperz · Oct 21, 2013 at 11:55 PM 0
Share

I should have clarified, updating the original post. It is attached to the prefab that is my bullet. So, all of these happen when my bullet experiences a collision. There is only one script.

avatar image Benproductions1 · Oct 22, 2013 at 12:17 AM 0
Share

You don't ever have to duplicate code. You should be doing something like this ins$$anonymous$$d of duplicating the same code for every box:

 function OnCollisionEnter(theCollision:Collision) {
     boxHit1 = CheckBoxCollision(theCollision, "Box1", boxHit1);
     boxHit2 = CheckBoxCollision(theCollision, "Box2", boxHit2);
     boxHit3 = CheckBoxCollision(theCollision, "Box3", boxHit3);
 }
 
 function CheckBoxCollision(collision:Collision, name:String, boxHit:boolean):boolean {
     if (boxHit && collision.gameObject.name == name) {
         //all your code in relation to 'collision', 'name' and 'boxHit'
     }
     return boxHit;
 }

This makes for much nicer and more readable code :)

1 Reply

· Add your reply
  • Sort: 
avatar image
0

Answer by Slipperz · Oct 22, 2013 at 12:19 AM

Well, I figured it out. Thanks to rutter I realized a single individual bullet would have to hit all three for this to work. So right in front of my face. Thank you! I suppose the next line of action would be to tell a separate script to shift booleans on an gameObject that doesn't destroy itself.

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

17 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

Related Questions

Simple GUI Problem... 3 Answers

Animation gets stuck in the last frame for 2 secs. 1 Answer

Export objects to a .3DS file at runtime 1 Answer

How to convert new unity input system input actions to booleans 1 Answer

How do I format a string into days:minutes:hours:seconds? 6 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