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.

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.