• 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 Sakulex · Mar 27, 2014 at 07:29 AM · collisionarraysactivation

Triggering a Transform to Active on Collision

I'm a complete noob when it comes to scripting, and am having difficulty in triggering one of four different objects upon collision.

Long story short, I'm creating a card game where players place cards in a cauldron. The cauldron displays one of four 'elemental states', which the players change with whatever value is attached to the card they play when it lands in the cauldron.

I have the 'elementEffects' in an array (they need to be; it's part of my assessment), and they are all set to inactive in Unity.

2 things I need help with:

1 - I'm not sure how declare the 'collider' variable, as the cards are varied 2 - How to switch one effect off and the next one on with the next collision

Code attached to the Cauldron follows (nb: elementEffects is 'i-1' as the CardSuits are numbered 1 to 4, not 0 to 3):

 var elementEffects : Transform[];
 
 function OnCollisionEnter(collision : Collision) 
 {
     var i = collider.GetComponent(CardValue).cardSuit;
     elementEffects[i-1].active = true;     
 }

Thanks heaps in advance!

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

1 Reply

· Add your reply
  • Sort: 
avatar image
0

Answer by Leuthil · Mar 27, 2014 at 01:11 PM

You can access the correct collider by using collision.collider. You don't really need a 'collider' variable, unless I'm misunderstanding you.

To set them from active to inactive when another collision is made, I would personally loop through all elementEffects and set them all to inactive, and then set the correct one to active every time a collision is made. This should ensure the desired effect (no pun intended). The other option is to store a variable which stores the last elementEffect that was set to active so you can directly set it to inactive, and then assign the new elementEffect to that variable.

So your script would change to:

 var elementEffects : Transform[];
  
 function OnCollisionEnter(collision : Collision) 
 {
     DeactivateElementEffects();
     var i = collision.collider.GetComponent(CardValue).cardSuit;
     elementEffects[i-1].active = true;  
 }

 private void DeactivateElementEffects()
 {
     for (var i = 0; i < elementEffects.length; i++)
     {
         elementEffects[i].active = false;
     }
 }

Or:

 var activeEffect : Transform;
 var elementEffects : Transform[];
  
 function OnCollisionEnter(collision : Collision) 
 {
     var i = collision.collider.GetComponent(CardValue).cardSuit;
     activeEffect.active = false;
     activeEffect = elementEffects[i-1];
     elementEffects[i-1].active = true;
 }

I haven't tested this code and since I usually use C# it may be incorrect, but you should get the idea.

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.

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

20 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

Related Questions

RayCast Collision between 2 game objects (of the same prefab) 1 Answer

Turning off single object in an array 2 Answers

how do i make a 2d array of collision boxes 1 Answer

Activating Objects with collision of another objects 1 Answer

Find colliders after collision? 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