• 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
Question by CrisisMode · Jun 20, 2014 at 08:21 PM · 2dcollisioncolorplatformercolider

Player color change on collision

I have a CharacterController2D script in which I placed a public transform of playerColor1, 2, and 3. I made a prefab for each of these states and dragged them into my player in the editor window. I am just learning by the way and I am sort of just winging it here to see if I can do this. So that part is finished and here is that part of the controller script:

 using UnityEngine;
 using System.Collections;
 
 public class CharacterController2D : MonoBehaviour {
 
     private const float SkinWidth = .02f;
     private const int TotalHorizontalRays = 8;
     private const int TotalVerticalRays = 4;
     
     private static readonly float SlopeLimitTangent = Mathf.Tan(75f * Mathf.Deg2Rad);
 
     public Transform playerColor, playerColor2, playerColor3;
 
     public LayerMask PlatformMask;
     public ControllerParameters2D DefaultParameters;
 
     public ControllerState2D State { get; private set;}
     public Vector2 Velocity {get{return _velocity;}}
     public bool CanJump{

Now, I want to make cubes throughout the level that will transform the player character into one of those given color states depending on the color of the cube the player collides with so I wrote the following script:

 using UnityEngine;
 using System.Collections;
 
 public class ColorChange : MonoBehaviour {
 
     public Transform myColor;
 
     public CharacterController2D _controller;
 
     void Awake() {
 
         _controller = GetComponent<CharacterController2D> ();
     
     }
 
     public void OnCollisionEnter2D(Collision2D col){
 
         CharacterController2D controller = col.gameObject.GetComponent<CharacterController2D>();
 
         if (controller != null) {     // collided with character controller
         if (myColor != controller.playerColor)
                 controller.playerColor = myColor;
 
         else{
             if(myColor = controller.playerColor)
                     return;
 
         }
     }
 }
 }

I have no errors and I was pretty proud of myself because it seems that everything is all the way it should be, except that when I collide with said cube, nothing happens. So I must be missing something, probably a pretty big part. Do I need to instantiate the controller in its new color after collision? I really don't know what is missing here. The actual method of switching the character's transforms apparently.

I could really use the help because this is a game mechanic I am really interested in implemening in my platformer.

Thanks.

Comment

People who like this

0 Show 1
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 CrisisMode · Jun 21, 2014 at 04:48 PM 0
Share

I have used the console and the player cube seems to know its color as does the color change cube. I tried instantiating the myColor and that does work sort of, but the new cube appears in the scene view and not the game view.

1 Reply

  • Sort: 
avatar image

Answer by Jeff-Kesselman · Jun 20, 2014 at 08:25 PM

You have a good start here, but here is one basic error:

 if(myColor = controller.playerColor)

is wrong, it should be

 if(myColor == controller.playerColor)



Single equals is assignment, dual equals is comparsion.

Also, a piece of advice, dont use naked ifs (ifs not followed by { }). It will just confuse your code. Always use {} after ifs or loop constructs, even if the body is only one statement.

Trust an experienced programmer, if you don't, it WILL eventually bite you.

So, with that error fixed and your code cleaned up a biut it would look like:

 using UnityEngine;
 using System.Collections;
  
 public class ColorChange : MonoBehaviour {
  
     public Transform myColor;
  
     public CharacterController2D _controller;
  
     void Awake() {
  
         _controller = GetComponent<CharacterController2D> ();
  
     }
  
     public void OnCollisionEnter2D(Collision2D col){
  
         CharacterController2D controller =   
                 col.gameObject.GetComponent<CharacterController2D>();
  
         if (controller != null) {     // collided with character controller
             if (myColor != controller.playerColor){
                 controller.playerColor = myColor;
  
             } else {
                 if(myColor == controller.playerColor) {
                     return;
                 }
             }
         }
     }
 }
 

Note that once you have it organized you can see that the second if is in fact redundant and can be removes, making your code:

 using UnityEngine;
 using System.Collections;
 
 public class ColorChange : MonoBehaviour {
 
     public Transform myColor;
 
     public CharacterController2D _controller;
 
     void Awake() {
 
         _controller = GetComponent<CharacterController2D> ();
 
     }
 
     public void OnCollisionEnter2D(Collision2D col){
 
         CharacterController2D controller =   
                 col.gameObject.GetComponent<CharacterController2D>();
 
         if (controller != null) {     // collided with character controller
             if (myColor != controller.playerColor){
                 controller.playerColor = myColor;
 
             } else {
                 return;
             }
         }
     }
 }

Having done that, you might notice that your return is also redundant as, if it weren't there, it would just drop out the bottom and return anyway. So you can eliminate that too and further simplify your code:

 using UnityEngine;
 using System.Collections;
 
 public class ColorChange : MonoBehaviour {
 
     public Transform myColor;
 
     public CharacterController2D _controller;
 
     void Awake() {
 
         _controller = GetComponent<CharacterController2D> ();
 
     }
 
     public void OnCollisionEnter2D(Collision2D col){
 
         CharacterController2D controller =   
                 col.gameObject.GetComponent<CharacterController2D>();
 
         if (controller != null) {     // collided with character controller
             if (myColor != controller.playerColor){
                 controller.playerColor = myColor;
             } 
         }
     }
 }

Once you've done that, if it still isnt happening, you need to see if the collision routine is being called at all, so put a Debug.Log at the top:

 using UnityEngine;
 using System.Collections;
 
 public class ColorChange : MonoBehaviour {
 
     public Transform myColor;
 
     public CharacterController2D _controller;
 
     void Awake() {
 
         _controller = GetComponent<CharacterController2D> ();
 
     }
 
     public void OnCollisionEnter2D(Collision2D col){
         Debug.Log("Collided with "+col.gameObject.name);
         CharacterController2D controller =   
                 col.gameObject.GetComponent<CharacterController2D>();
 
         if (controller != null) {     // collided with character controller
             if (myColor != controller.playerColor){
                 controller.playerColor = myColor;
             } 
         }
     }
 }

Let us know the result of that much.

Comment

People who like this

0 Show 2 · 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 CrisisMode · Jun 20, 2014 at 09:33 PM 0
Share

That didn't really seem to do anything. I should mention perhaps, in case I haven't, that I made a prefab of each character's colors and dropped the prefabs into the public transform locations.

I also set the box collider as a trigger which I believe I should do? Either way, the player cube just goes right through the object with no results at all. What I thought would happen is that if I set the color change object to purple, that it would change the player cube to its purple prefab.

avatar image CrisisMode · Jun 21, 2014 at 10:23 AM 0
Share

Okay, so I do have it colliding with the object I wish to have initialize the color change. I used a trigger instead that way I can have an on trigger exit to make it disappear and I have it reappearing after I die. However, it is not actually changing the color of my player :(

Unity Answers is in Read-Only mode

Unity Answers content will be migrated to a new Community platform and we are aiming to launch a public beta by June 9. Please note, Unity Answers is now in read-only so we can prepare for the final data migration.

For more information and updates, please read our full announcement thread in the Unity Forum.

Follow this Question

Answers Answers and Comments

3 People are following this question.

avatar image avatar image avatar image

Related Questions

Player pass each other laterally but can step on each other 0 Answers

2D - Detect Slopes x Walls 1 Answer

Pushing against Edge Collider wall stops player from falling 0 Answers

2d Platformer Jumping Not Working 1 Answer

Are Character Controllers inefficient? 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