- Home /
Instantiate not behaving as expected with enabling/disabling Collider
Attempting to add a invincibility period script, when the player is Destroyed a respawn sequence begins. Respawn works fine however the collider remains disabled and doesn't reenabled after it Instantiates the player. Only time enabling the collider works if the enable code is before the Instantiate code, but that doesn't allow for brief invincibility after respawn.
using UnityEngine;
using System.Collections;
public class Spawn : MonoBehaviour {
public GameObject Player;
public static bool alive;
Collider Safety;
void Start ()
{
Vector3 respawnPoint = new Vector3 (0, 0, 0); //Where the player intially spawns
Instantiate (Player, respawnPoint, Quaternion.identity);
alive = true; //Player Status is Alive
}
void Update ()
{
if (alive == false) //If Player is Destroyed(Status Changed to False by DestroyByContact Script)
{
StartCoroutine(respawn()); // Begin Respawn Sequence
alive = true; //Reset layer to Alive Status
}
}
IEnumerator respawn()
{
RespawnCounter.respawning = true; //Enables respawning Text UI in RespawnCounter Script
yield return new WaitForSeconds (3.0f); //Wait 3 seconds before respawning
RespawnCounter.respawning = false; //Disables Respawning Text UI
Safety = Player.GetComponent<Collider>();
Safety.enabled = false; //Disables Collider for intial respawn Safety
Debug.Log (Safety.enabled);
Vector3 point = new Vector3 (0,0,0); //Respawns player back at Starting point
Instantiate(Player, point, Quaternion.identity);
yield return new WaitForSeconds(1.5f); //Enables Collider after 1.5 seconds
Safety.enabled = true;
Debug.Log (Safety.enabled);
}
}
Answer by SanX91 · Jan 16, 2015 at 04:49 AM
public class Spawn : MonoBehaviour {
public GameObject Player;
public GameObject playerPrefab;
public static bool alive;
Collider Safety;
void Start ()
{
Vector3 respawnPoint = new Vector3 (0, 0, 0); //Where the player intially spawns
Player = Instantiate (playerPrefab, respawnPoint, Quaternion.identity) as GameObject;
alive = true; //Player Status is Alive
}
void Update ()
{
if (alive == false) //If Player is Destroyed(Status Changed to False by DestroyByContact Script)
{
StartCoroutine(respawn()); // Begin Respawn Sequence
alive = true; //Reset layer to Alive Status
}
}
IEnumerator respawn()
{
RespawnCounter.respawning = true; //Enables respawning Text UI in RespawnCounter Script
yield return new WaitForSeconds (3.0f); //Wait 3 seconds before respawning
RespawnCounter.respawning = false; //Disables Respawning Text UI
Safety = Player.GetComponent<Collider>();
Safety.enabled = false; //Disables Collider for intial respawn Safety
Debug.Log (Safety.enabled);
Destroy(Player);
Vector3 point = new Vector3 (0,0,0); //Respawns player back at Starting point
Player = Instantiate(playerPrefab, point, Quaternion.identity) as GameObject;
yield return new WaitForSeconds(1.5f); //Enables Collider after 1.5 seconds
Safety = Player.GetComponent<Collider>();
Safety.enabled = true;
Debug.Log (Safety.enabled);
}
}
Try this and check.. Make a player prefab first instead.. Just a quick review... Things can be further improved if needed.
$$anonymous$$oved lines 29 and 30 to after Instantiating the playerPrefab and Removed the Destroy(Player) line since another script does that and it worked. Thanks a bunch! Do you know why when I tried doing " Player = Instantiate(playerPrefab, point, Quaternion.identity) as Collider; " it didn't work as expected?
$$anonymous$$y guess is that I need to store my playerPrefab info into Player first then access the Collider, can't jump straight to accessing the Collider like what I tried.
yup, u're right. when a prefab is instantiated u can't directly access it unless u pass it in a variable.. That's because u won't have any reference to it within the script.
This line instantiates your player:
Instantiate(Player, point, Quaternion.identity);
but u can't really do anything more with that instantiated object within that particular script, unless u do this:
player = Instantiate(playerPrefab, point, Quaternion.identity) as GameObject;
Now, the player is under your control
Your answer
Follow this Question
Related Questions
Multiple Cars not working 1 Answer
Distribute terrain in zones 3 Answers
how do i add a offset to my sword rotation? 2 Answers
How to make a GameObject a button? 2 Answers
audio doesn't play on movement 0 Answers