• 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 ococo · Mar 10, 2014 at 03:15 PM · collisionslocalscale

transform.localScale affects all instances at once

Hi all, I am trying to make a very simple game similar to the iOS title Circle Stop. I am quite new at coding so even that is difficult for me :( I have a player moving automatically and several pickups on his path. All pickups are instances of the same prefab.

When the player is hitting one pickup, I want that pickup to scale up. I am using the code below. I have a script on the player to detect collisions (ColCheck). I have a script on the pickup to transform its scale. basically, the pickup script checks if the collision happened and scales the object.

 function Update () {  
         if (hintCheck == ColCheck.pickupHit) {
             transform.localScale = Vector2(2, 2);
         }
     }

the variables hintCheck and pickupHit allow me to see how many pickups were hit. Each pickup has a different value. The first has hintCheck = 1, the second hintCheck = 2 and so on... This way, each pickup should react independently.

As soon as the player hits the first pickup, all are scaled. This really surprised me as I used before transform.position and that only affected one pickup at a time.

Anybody knows what I am doing wrong?

Comment
Add comment · Show 3
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 suribe · Mar 10, 2014 at 03:18 PM 0
Share

Could you show also the parts where hintCheck & pickupHit are assigned their values? Also, are you instancing this prefab in code, or are you just dropping it from the editor and it is blue?

avatar image ococo · Mar 10, 2014 at 03:49 PM 0
Share

Sure, This is the code for the player.

   function OnTriggerEnter2D(theCollision : Collider2D) {
         if(theCollision.gameObject.tag == "pickup"){
             pickupHit += 1;        
         }
     }

I also used onCollisionEnter2D, with the same result.

I dropped the instances from the project, and each pickup is blue.

avatar image ococo · Mar 10, 2014 at 04:19 PM 0
Share

But each object has a different hintCheck value. The first pickup will trigger when pickupHit = 1 and so on... So that condition can't be true for each pickup at once. This worked well for transform.position (only the pickup hit moved). I don't understand why it's different with localScale.

2 Replies

· Add your reply
  • Sort: 
avatar image
0

Answer by highpockets · Mar 10, 2014 at 03:19 PM

You have this script on every instance and so when hintCheck == pickUpHit they will all scale. Each of these objects is checking this condition every update.

Comment
Add comment · Show 7 · 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 highpockets · Mar 10, 2014 at 03:21 PM 0
Share

A better solution would be to use:

 function OnCollisionEnter( collision : Collision )

Or:

 function OnTriggerEnter( collision : Collision )
avatar image suribe · Mar 10, 2014 at 04:21 PM 0
Share

If he has setup hintCheck different for each, this should not happen, only one should react. But you are right that using OnCollisionEnter or OnTriggerEnter should be a better (and easier) solution.

avatar image ococo · Mar 10, 2014 at 11:16 PM 0
Share

I did add that condition check to OnCollisionEnter rather than on the pickup script. I got the same result

avatar image ococo · Mar 10, 2014 at 11:39 PM 0
Share

I think that I have found the origin of my problem, but still not a solution. It seems that the collision is detected several times, incrementing the pickupHit variable more than once.

When the player hits the first pickup, pickupHit = 2. I would expect it to be 1.

I am using OnTriggerEnter. $$anonymous$$y objects are sprites, with a circle collider and a box collider. Any idea?

avatar image highpockets · Mar 11, 2014 at 10:57 PM 0
Share

$$anonymous$$aybe OnCollision/TriggerEnter you could disable the collider, that way, it could no longer call the collision function after the first hit. $$anonymous$$aybe as you scale up, it considers it a collision/trigger enter at every incremental change of the object. It would definitely be a problem if it was oncollision/triggerstay as the collider would be getting bigger and continuing to hit the player. Anyways, you can turn off the collider of the prefab like so:

 gameObject.transform.collider.enabled = false;

Show more comments
avatar image
0

Answer by suribe · Mar 10, 2014 at 04:53 PM

Try dettaching the objects from the Prefabs. Go to the Hierarchy and select the object. Then in the GameObject menu, click "Break Prefab Instance". The problem with this is that any change you make to the prefab is no longer valid for these instances. You can solve that by creating instances on code through the GameObject.Instantiate function.

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 ococo · Mar 10, 2014 at 11:18 PM 0
Share

Thanks. I tried that but with the same effect.

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.

Welcome to Unity Answers

If you’re new to Unity Answers, please check our User Guide to help you navigate through our website and refer to our FAQ for more information.

Before posting, make sure to check out our Knowledge Base for commonly asked Unity questions.

Check our Moderator Guidelines if you’re a new moderator and want to work together in an effort to improve Unity Answers and support our users.

Follow this Question

Answers Answers and Comments

22 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

Related Questions

Issue with changing localScale of gameobject after instantiate 1 Answer

Unity 4.5 LocalScale Changes (2D) 1 Answer

LocalScale doesn't scale sprite to the distance specified 0 Answers

Scaling object by increasing its y position 1 Answer

How to Lerp localScale? 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