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
1
Question by zinnfandel · Nov 24, 2011 at 02:02 PM · collisiongetcomponent

Problem getting information out of colliding objects

Hello, I have a scene where there are many objects of different size. The plays runs around bumping into them. I need a different reaction if the objects are larger or smaller. Calculating their size and player's size is no problem, I tested and saw they all have a different value. but when I get to the collision test, I get stuck on the same piece of data from some object, and it is not re-checking the data for all objects.

This is my collision code:

 function OnCollisionEnter(hit : Collision)
 {
 if(hit.gameObject.tag == "object")
     {
         var colliderSize = hit.gameObject.GetComponent(ObjectControl).objectSize;
 
         
         if (colliderSize < playerSize)
             {
             Destroy(hit.gameObject); // if object is bigger, change here to push the player or some type of feedback + sound
             playerSize +=1;
             print("Yummy! I ate a " + colliderSize+ " and my size is now: " + playerSize);
             }
 
         else
             {
             Destroy(hit.gameObject); // if object is bigger, change here to push the player or some type of feedback + sound
             playerSize -=1;
             print("I've been hit! " + colliderSize + " by a " + colliderSize);
             }
     
 }
 }


This is the bit of code from each object. They change colour depending if they are smaller or larger than the player's current size:

 static var objectSize = 1.0;
 var swapMat : Material;
 var originalMat : Material;
 
 function Update () 
 {
     var Player = gameObject.Find("player");
     var PlayerSize = Player.GetComponent(PlayerInteractions).playerSize;
     
     objectSize =  (transform.localScale.x);
     
     if (objectSize > PlayerSize) //object is larger than player
     {
     // paint object red
     renderer.material = swapMat;
   
     }
     
     if (PlayerSize > objectSize ) //if smaller than player 
     {
     //paint object green
     renderer.material = originalMat;
 
     }
     
 
 }
Comment
Add comment · Show 9
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 zinnfandel · Nov 30, 2011 at 10:45 PM 0
Share

Please, any ideas anyone?

avatar image gfr · Nov 30, 2011 at 11:23 PM 0
Share

When you inspect the objects in the inspector at runtime, are the object sizes actually different? Are you actually getting different objects every hit? If so, how?

avatar image zinnfandel · Nov 30, 2011 at 11:28 PM 0
Share

Yes, the objects sizes are different. I'm adding the bit of code from each object above. They change colour depending wether they are smaller or larger than the player's current size, and that part works fine.

avatar image gfr · Nov 30, 2011 at 11:33 PM 0
Share

I guess that only leaves the hit being wrong - where does that come from?

avatar image zinnfandel · Nov 30, 2011 at 11:34 PM 0
Share

After more testing, it appears it always gets stuck on a certain object, same one every run, and until the player collides with it (and destroys it). it takes the data from it. Afterwards it gets stuck on another object's data.

Show more comments

1 Reply

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

Answer by aldonaletto · Dec 01, 2011 at 12:29 AM

You should remove static from the objectSize declaration: a static variable is unique, no matter how many instances of the script where it's declared exist - and you must have an independent objectSize variable in each object's script. Being static, each object will modify its value in Update, and only God knows which size the player will see when colliding to an object.
Another suggestion: find the player only once at Start - Find is a slow operation, and may reduce the framerate:

var objectSize = 1.0; // <- remove the static keyword var swapMat : Material; var originalMat : Material; private var Player : GameObject; // make Player a non-temporary variable

function Start (){ // find the player only once at Start Player = gameObject.Find("player"); }

function Update (){ var PlayerSize = Player.GetComponent(PlayerInteractions).playerSize; objectSize = transform.localScale.x; ...

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 gfr · Dec 01, 2011 at 12:32 AM 2
Share

I guess i used my blind eye when reading that... Good catch. By the way, why not cache the reference to the component?

avatar image zinnfandel · Dec 01, 2011 at 12:35 AM 0
Share

I see! Thank you, been breaking my neck on this. And thanks for the extra enlightenment :)

avatar image aldonaletto · Dec 01, 2011 at 12:44 AM 0
Share

@gfr gave a good suggestion, @zinnfandel - ins$$anonymous$$d of storing the GameObject "player", you could cache the script reference:

 ...
 var original$$anonymous$$at : $$anonymous$$aterial;
 private var playerScript: PlayerInteractions;

 function Start(){
     var player = GameObject.Find("player");
     playerScript = player.GetComponent(PlayerInteractions);
 }

 function Update (){
     var PlayerSize = playerScript.playerSize;
     ...

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

Change NavMesh Speed after collision/trigger just for 1 object 1 Answer

How to activate parent camera of gameobject -1 Answers

Calling methods on other Game Objects 1 Answer

Variables from one script to another. 3 Answers

acting on other object during collision: SendMessage or GetComponent? 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