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 Shuher · May 11, 2015 at 03:17 PM · javascriptontriggerenter

All objects gets same val. But i dont want that

hi. i have two planets, with gravitational field, and some space ships. when space ships entering gravitational field, it must send +1 to gravitation script. it working, BUT! both gravitational fields gets +1. why?

 private var gravScript : gravitation;
 
 function OnTriggerEnter (planetGrav : Collider)
 {
     
     if(planetGrav.name == "grav")
     {
            gravScript = planetGrav.GetComponent(gravitation);        
            gravScript.unitsCount++;
         
     }
 
 }
Comment
Add comment · Show 18
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 $$anonymous$$ · May 11, 2015 at 04:00 PM 0
Share

Did you make sure, that both Planet Colliders aren't touching each other so they dont get triggered both at the same time?

avatar image Shuher · May 11, 2015 at 04:11 PM 0
Share

they are about 140 unity units away from each other. it's planets) they can't be close)

avatar image Bonfire-Boy · May 11, 2015 at 04:56 PM 0
Share

Their sizes are relevant to that question as well as their locations. I$$anonymous$$O the only way to be 100% sure is to put logging in the OnTriggerEnter function so you can see how many times and in relation to what pair(s) of objects it is called.

avatar image Shuher · May 12, 2015 at 07:55 AM 0
Share

when i changing name in second gravitation field from "grav" to any else, second gravitation field still gets vals like in first field. i think gravScript = planetGrav.GetComponent(gravitation); gets vars not only from from planetGrav collider, but from all objects that have my script(gravScript : gravitation) attached. is it possible?

upd: yes, i just attached this script to another object, and it geting all the same vals as original gravitation field.

avatar image Bonfire-Boy · May 12, 2015 at 11:15 AM 1
Share

So the next thing I would do is look at is how when and from where those unitsCount values are getting changed.

Ins$$anonymous$$d of simply incrementing them, have a function called incrementUnitCount on the gravitation script which not only does the increment but also logs that it's been called (together with the value in question as well as the name of the object). $$anonymous$$ake sure nothing else is changing that value.

Show more comments

2 Replies

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

Answer by Shuher · May 12, 2015 at 11:54 AM

it works!!! before function incrementUnitCount in gravitation script, i used "static var" for unitsCount. so now i removed "static" and it works!!)))

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
avatar image
0

Answer by Eudaimonium · May 12, 2015 at 01:03 PM

Can you post the code of your Gravitation script/class/struct?

Also, to what object is this script attached, I assume the spaceship?

It seems to me you marked the "unitsCount" as "static", so the same variable is shared between all instances of the class that have it.

You need to remove the "static" keyword from there, if you have it. If not, I assume we'll find the problem in Gravitation.

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 Shuher · May 12, 2015 at 01:14 PM 0
Share

yes. the problem was in static var. ty

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

On collision between two cubes 2 Answers

Play Animation on trigger enter 1 Answer

Setting Scroll View Width GUILayout 1 Answer

What is the easiest way to make an object notice another? 3 Answers

Cueing an Animation From Another GameObject's Script (Javascript) 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