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”;
}
}
}