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.