- Home /
HELP, Scripts dont work after instantiation.
Hey. Im going to cut this short. The game is a 1v1 melon bomber. When the objects are killed, they are destroyed. Then a respawn script is triggered and the gameobjects are spawned but they wont move. HELP, no forum post helped.
SpawnManager:
public class SpawnManager : MonoBehaviour {
public GameObject PlayerOne;
public GameObject PlayerTwo;
public static GameObject Player1Prefab;
public static GameObject Player2Prefab;
public Transform PlayerOneSpawn;
public Transform PlayerTwoSpawn;
void Start () {
Spawn ();
}
void Update () {
if (ScoreManager.ReSpawn == 1) {
Spawn ();
ScoreManager.ReSpawn = 0;
}
}
// Update is called once per frame
public void Spawn () {
Player1Prefab =(GameObject) Instantiate(PlayerOne, PlayerOneSpawn.transform.position, PlayerOneSpawn.transform.rotation);
Player2Prefab =(GameObject) Instantiate(PlayerTwo, PlayerTwoSpawn.transform.position, PlayerTwoSpawn.transform.rotation);
}
}
PlayerHealth (Second player is the exact same):
public class PlayerHealth : MonoBehaviour {
public static int currentHealth;
void Awake () {
currentHealth = 100;
}
void Update () {
Debug.Log (currentHealth);
if (currentHealth <= 0){
Debug.Log ("SCOOORE");
ScoreManager.PlayerTwoScore = ScoreManager.PlayerTwoScore + 1;
ScoreManager.NextRound = true;
DestroyThis();
}
if (ScoreManager.NextRound == true) {
DestroyThis();
}
}
void DestroyThis () {
Destroy(this.gameObject);
}
}
Movement Script:
public class PlayerOneController : MonoBehaviour {
public float speed = 1.5f;
public GameObject PlayerOneBomb;
public GameObject PlayerOneDamageR;
public static int BombsOne;
void OnEnable () {
BombsOne = 1;
}
void Update ()
{
if (Input.GetKey(KeyCode.A))
{
transform.position += Vector3.left * speed * Time.deltaTime;
}
if (Input.GetKey(KeyCode.D))
{
transform.position += Vector3.right * speed * Time.deltaTime;
}
if (Input.GetKey(KeyCode.W))
{
transform.position += Vector3.up * speed * Time.deltaTime;
}
if (Input.GetKey(KeyCode.S))
{
transform.position += Vector3.down * speed * Time.deltaTime;
}
if (Input.GetKey (KeyCode.Space)) {
if(BombsOne >= 1){
BombsOne= BombsOne - 1;
Instantiate(PlayerOneBomb, this.transform.position, this.transform.rotation);
Instantiate(PlayerOneDamageR, this.transform.position, this.transform.rotation);
StartCoroutine(Resupply());
}
}
}
IEnumerator Resupply (){
yield return new WaitForSeconds (2);
BombsOne = BombsOne + 1;
}
}
Thanks
With your prefab, are these 2 scripts (your movement and health) attached to it? When you have a prefab and you have variables you want to essentially "drag and drop" into the inspector, it will only look at Asset objects, so essentially other files or prefabs, not objects in the active scene as the Inspector normally would with game objects. So you would either need to default these public variables so that the script can still work properly once re-spawned, or re-set these variables.
Also make sure that both scripts are actually enabled on your prefab.
Your "Spawn" mothod seems to be a problem.
Player1Prefab =(GameObject) Instantiate(PlayerOne, PlayerOneSpawn.transform.position, PlayerOneSpawn.transform.rotation);
It should probably look more like
PlayerOne =(GameObject) Instantiate(Player1Prefab, PlayerOneSpawn.transform.position, PlayerOneSpawn.transform.rotation);
Instantiating is creating an scene object of prefab, so the prefab object should be a "source" for this operation- that's why you should pass it as parameter and return the result of instantiating (which is spawned scene object) to your PlayerOne field
Your answer
![](https://koobas.hobune.stream/wayback/20220612082844im_/https://answers.unity.com/themes/thub/images/avi.jpg)