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 8BitSandy · May 26, 2013 at 02:28 PM · collisionifglobal variables

Specific collision applying to all collisions in a script...

I have a script to change a variable when an object collides with an other certain object, but it is applying to all collisions that happen to it. Here is the code.

function OnCollisionEnter(collision : Collision){ if(collision.gameObject.tag == "MainChar" && Walking.runspeed > 8); Walking.runspeed = Walking.runspeed - 7; yield WaitForSeconds (waittime); Walking.runspeed = Walking.runspeed + 7; }

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
0

Answer by Owen-Reynolds · May 26, 2013 at 02:43 PM

Semi-colon after the if shouldn't be there. If you get a chance, this is the sort of thing any so-so intro programming book will cover. I've had people say the "Head First" books aren't bad. The rules for ifs are pretty much the same for all languages:

 if( ... )    // <-- no semi-colon
 {            // <- curlybrace marks start of if code
    ...
 }             // <- close curly marks end of if code

You think, "but wait, semicolons go at the end of all statements." The thing is, the IF isn't done yet. A semi-colon says "if this is true...nevermind." Then the stuff you meant to be in the if, becomes just normal "do all the time" code.

Likewise, the code inside needs curly-braces. When the computer sees "if(A) then B; C;" it can't tell whether C is part of the if, or "now the if is done, keep going." If you leave out the {}'s, the computer just guesses you meant to have them around only B.

Comment
Add comment · Show 3 · 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 8BitSandy · May 26, 2013 at 04:44 PM 0
Share

Thanks, my main problem is gone, but now it doesn't slow at all.I think the problem is with the if conditions. I have used the exact name of the gameObject, so I don't know why it is happening though.

avatar image Owen-Reynolds · May 26, 2013 at 04:50 PM 0
Share

Try with only speed-7. I'm not sure you can use yield in an OnCollision. But even if you can, test out simple stuff first. Put in Debug.Log("slow"); to double-check (or whatever javascript uses.)

avatar image 8BitSandy · May 26, 2013 at 05:35 PM 0
Share

Didn't wok :( No change

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

14 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

Related Questions

Nested if statement only runs once. I don't know why. 1 Answer

Detect collision if 1 Answer

How to remove script on collision? 2 Answers

"if(Touch )" statement in Unity script? Is it Possible?! 1 Answer

Why my function not working with less than velocity?,why my function not work with an If statements with a greater/less than 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