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 Ilkzz · Jun 11, 2014 at 10:06 PM · collisionreference

Value to 0 on collision

Hi,

Very simple problem which I can't seem to solve and I haven't got the slightest idea why this does not work.

I have a variable called speed in one script

When my player collides, I want the speed to be 0, so it stops moving. I have 3 objects moving at different speeds using the same code (speeds are set in the inspector)

 bgMove.speed = 0f;

The above code doesn't seem to do anything, and it is under OnCollisionEnter2D()

Am I doing it wrong?

Thanks

Comment
Add comment · Show 7
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 christoph_r · Jun 11, 2014 at 10:40 PM 0
Share

Please post your entire OnCollisionEnter2D() method.

avatar image psyydack · Jun 11, 2014 at 11:02 PM 0
Share

Your OnCollisionEnter is working? Can you have access that variable?

avatar image Ilkzz · Jun 22, 2014 at 10:08 PM 0
Share

These are my 2 functions that I use.

hgmove is another script that I use with a simple variable speed

 void OnCollisionEnter2D(Collision2D other)
         {
             collider2D.enabled = false;
             Die ();
         }

 void Die()
     {
         bg$$anonymous$$ove.speed = 0f;
         isDead = true;
     }


avatar image BsseeJ · Jun 22, 2014 at 10:38 PM 0
Share

Did you mistype hgmove and meant to say bg$$anonymous$$ove, because if bg$$anonymous$$ove is on a different script that is probably your problem.

avatar image Kiwasi · Jun 22, 2014 at 10:42 PM 0
Share

Use Debug.Log statements to check what is being called. Is your collision actually happening?

Show more comments

1 Reply

· Add your reply
  • Sort: 
avatar image
0

Answer by meat5000 · Jun 22, 2014 at 10:41 PM

To hazard a guess, you need to 'Find' the relevant gameObjects and use GetComponent to access, or cache, bgMove.

Once it is cached you can then access the speed variable with the dot operator through your instance

bgMoveInstance.speed = 0f;

Comment
Add comment · Show 4 · 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 Ilkzz · Jun 23, 2014 at 09:11 PM 0
Share

I am using GetComponent to access the variable. I think I have found my problem. The bg$$anonymous$$ove variable is run in Update().

 public float speed;
 
     void Update() {
         transform.position += (Vector3.left * speed) * Time.deltaTime;    
     }

Do you mean making it like this?

 public float speed;
 
         void Start() {
                 speed = 0.5;
         }
 
     void Update() {
         transform.position += (Vector3.left * speed) * Time.deltaTime;    
     }
avatar image meat5000 ♦ · Jun 23, 2014 at 11:49 PM 0
Share

I'm pretty weak in C# so bear with me :)

 private gameObject bgObject = new GameObject;
 private bg$$anonymous$$ove bg$$anonymous$$oveInstance = new bg$$anonymous$$ove;
 
 void Start()
 {
      bgObject = GameObject.Find("Object-with-bg$$anonymous$$ove-on-it");
      bg$$anonymous$$oveInstance = bgObject.GetComponent(bg$$anonymous$$ove);
 }
 
 void Update()
 {
      Debug.Log(bg$$anonymous$$oveInstance.speed)
 }


I do apologise if its badly hashed nonsense but I'm sure you get the idea of what I'm doing here.

Find object with script you are trying to access and store it.

Use GetComponent to retrieve the script and store it in variable of the same type as your script. ($$anonymous$$y usage could be inaccurate there)

You can then use scriptInstance.variable to access you variable as if its in the same script :)

avatar image jefjohms · Jun 24, 2014 at 12:07 AM 0
Share

This looks like it should work. I would use the Unity drag feature for this ins$$anonymous$$d of find function but should do the same job.

avatar image meat5000 ♦ · Jun 24, 2014 at 12:57 AM 0
Share

Aye, I usually use FindWithTag, as it's cheaper than Find. I avoid Drag to $$anonymous$$imise NullRefs for whatever reason :) Sometimes can't be avoided though.

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

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

Related Questions

Reference to another script not working, not throwing any errors 0 Answers

Javascript score problems whilst referencing scripts. 2 Answers

Object reference not set to an instance of an object 1 Answer

how to call the script of an object being collided with a trigger2D 1 Answer

Get Object ID upon collision. 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