Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 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 kevinseligmann · Oct 03, 2011 at 08:56 AM · javascriptvariables

Accesing variable previously declared?

Hi everyone, I'm having a difficult time trying to make a very simple animation.

I have a sphere (testObject1) which I want to move left and right, and switch this movement when it collides to with a rigidbody.

So, I came up with this:


 var speed = 0.10;
 var dropPoint = 1;
 
 function Update () {
     
     if(dropPoint == 1){
         transform.Translate(0, 0, speed);
     }else{
         transform.Translate(0, 0, speed - (speed * 2));
     }    
 }
 
 function OnCollisionEnter(collision : Collision) {
     for (var contact : ContactPoint in collision.contacts) {
     
         if(collision.collider == "testObject3"){
             dropPoint = 2;
         }else{
             if(collision.collider == "testObject2"){
                 dropPoint = 1;
             }
         }
         if(collision.collider != "testFloor"){
             print(collision.collider);
         }
     }
 }
 


So, when I hit play, the sphere starts moving in Z direction, collides with the testObject3 and it keeps going in the same direction...

What am I doing wrong here?

Thanks!

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 Bunny83 · Oct 03, 2011 at 02:06 PM

Your problem is that you can't compare a Collider with a string!

I guess "testObject3" and "testObject2" are the names of those GameObjects, right? So you have to check the name of the GameObject:

 if(collision.collider.name == "testObject3"){
     dropPoint = 2;
 }
 else if(collision.collider.name == "testObject2"){
     dropPoint = 1;
 }

Another funny thing is your "`speed - (speed * 2)`" :D
It's the same as "`-speed`"

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 kevinseligmann · Oct 04, 2011 at 04:56 AM 0
Share

Thaaaaaaaaaanks very much! I guess I just imagine the worst way to do something and just do it that way :P Thanks!

avatar image
0

Answer by timsk · Oct 03, 2011 at 12:37 PM

try this:

  if(dropPoint == 1){
       transform.Translate(0, 0, speed);
    }else if(dropPoint ==2){
       transform.Translate(0, 0, speed - (speed * 2));
    }  
}

Debug the colliders too, make sure they are colliding correctly.

Also, instead of using

if(dropPoint ==2)

you might want to use

if(dropPoint >1)

Just for versatility.

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

4 People are following this question.

avatar image avatar image avatar image avatar image

Related Questions

One of my scripts can't find the variable from this script 1 Answer

Transform var between scripts 2 Answers

Making a List for Inventory System, Got an error in finding constructors and variables 1 Answer

Mob Spawner, choosing a random mob 2 Answers

Let's Discuss Efficiency (Tags Vs Variables) 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