I saved my script last night and it was working, Load this morning and it isn't working anymore.

I’m trying to make it so that when my player cube collides with another rigidbody it transfers control to the second block, effectively making that the player. I’m new to this so I’m not sure if this is the best way but it worked last night. after saving and reloading this morning it doesn’t do what it did last night. I can’t find anything wrong in the code and it isn’t showing any errors, so maybe there is a setting or something i’m missing in unity. any help would be appreciated. Thanks

using System;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class ControlSwitch : MonoBehaviour
{

private void OnCollisionEnter2D(Collision2D collision)
{
if (collision.gameObject.name == “Enemy”)
{

//On collision with an enemy, will add these components to an enemy
collision.gameObject.AddComponent();
collision.gameObject.AddComponent();

//destroy PlayerMovement and ControlSwitch from the previous target
Destroy(GetComponent());
Destroy(GetComponent());

//Name the object that is being hit the player and the old object an enemy
collision.gameObject.name = “Player”;
gameObject.name = “Enemy”;
}

}
}

Please use code tags: Using code tags properly

How to report your problem productively in the Unity3D forums:

http://plbm.com/?p=220

Also, review the requirements (in the Unity docs) for receiving collision callbacks such as OnCollisionEnter2D().

To help gain more insight into your problem, I recommend liberally sprinkling Debug.Log() statements through your code to display information in realtime.

Doing this should help you answer these types of questions:

  • is this code even running? which parts are running? how often does it run?
  • what are the values of the variables involved? Are they initialized?

Knowing this information will help you reason about the behavior you are seeing.

1 Like