0
votes

So I have this code to respawn my player when he hits his enemy. Everything works fine, but when the players spawn, he's uncontrolable. For some reason the Player clone has the controller script unchecked in the inspector.

Anyone have any idea as to why that happens & how to solve it?

using UnityEngine;
using System.Collections;

public class RedEnemy : MonoBehaviour {

GameObject spawnPoint; GameObject Player; // Use this for initialization void Start () { spawnPoint = GameObject.Find ("spawnPoint"); Player = GameObject.Find ("Player"); } //collider void OnTriggerEnter ( Collider other ){ if (other.tag == "Player") { Destroy (other.gameObject); GameObject Clone; Clone = Instantiate(Player, spawnPoint.transform.position, Quaternion.identity) as GameObject; } }

}

2

2 Answers

0
votes

Instead of destroying the player, you can simply reset their stats and move them.

Alternatively, you can enable the controller when the new player is spawned. Using whatever the name of your controller script is, you can do this:

Clone.GetComponent<Controller>().enabled = true;

You might also want to consider further your design. Having each enemy hold a reference to the spawn point and the player is a bit silly, and those Find operations will be taxing is you're spawning a large number of enemies. Ideally, since you only have one player and lots of enemies, you'd have the code for controlling the player in the player class, not the enemy.

0
votes

I think it is a bad idea to instantiate a Gameobject that isn't a prefab. You should only instantate prefabs and your code is instantiating an instance of a GameObject (sorry for the redundancy).

As your instance is being destroyed, that may result in an unexpected behaviour when trying to call Instantiate on it instead of on the prefab, like the one you are experiencing.

You should make a prefab of your player, then make your Player variable public and set it to the prefab of the Player in the inspector.