- Home /
Character respawns invisable?
Hey everyone im new to unity and i have made an enemy, done some basic ai using rain, have gave him health and gave him a respawn, but when he dies he respawns but he is invisible, it seems as though only the "character controller" along with its sensors, mind ect. the actual models arent coming back? please help :/
You need to show us some code of the respawn script. We can't really help you like that... $$anonymous$$aybe its disabling the mesh renderer. maybe you have an error... lots of stuff could be happening..
We don't have enough to go on. Pause the game after the enemy respawns and investigate.
What are you seeing that suggests he respawned at all?
Click on the enemy in the hierarchy. Does he have expected x, y, and z positions? $$anonymous$$aybe he isn't in your camera frustum?
Is the enemy's mesh present? Did you manipulate the mesh?
Any chance you moved a child object and not the parent, or maybe you moved the parent, but the child containing the mesh was displaced?
Is there anything that you're doing on the initial spawn to make the character appear as expected, that you're not doing on respawn?
This is a very open ended question. You'll need to provide more information. Good luck!
Answer by Benji-John93 · May 21, 2013 at 12:08 AM
///////////////////////////////////////////////////////////////////////////////// // // vp_DamageHandler.cs // © 2012 VisionPunk, Minea Softworks. All Rights Reserved. // // description: class for having a gameobject take damage, die and respawn. // any other object can do damage on this monobehaviour like so: // hitObject.SendMessage(Damage, 1.0f, SendMessageOptions.DontRequireReceiver); // /////////////////////////////////////////////////////////////////////////////////
using UnityEngine;
public class vp_DamageHandler : MonoBehaviour {
// health and death
public float Health = 10.0f; // initial health of the object instance, to be reset on respawn
public GameObject DeathEffect = null; // gameobject to spawn when object dies.
// TIP: could be fx, could also be rigidbody rubble
public float MinDeathDelay = 0.0f; // random timespan in seconds to delay death. good for cool serial explosions
public float MaxDeathDelay = 0.0f;
protected float m_CurrentHealth = 0.0f; // current health of the object instance
// respawn
public bool Respawns = true; // whether to respawn object or just delete it
public float MinRespawnTime = 3.0f; // random timespan in seconds to delay respawn
public float MaxRespawnTime = 3.0f;
public float RespawnCheckRadius = 1.0f; // area around object which must be clear of other objects before respawn
public AudioClip RespawnSound = null; // sound to play upon respawn
protected Vector3 m_StartPosition = Vector3.zero; // initial position detected and used for respawn
protected Quaternion m_StartRotation = Quaternion.identity; // initial rotation detected and used for respawn
///////////////////////////////////////////////////////////
//
///////////////////////////////////////////////////////////
void Awake()
{
m_CurrentHealth = Health;
m_StartPosition = transform.position;
m_StartRotation = transform.rotation;
}
///////////////////////////////////////////////////////////
// reduces current health by 'damage' points and kills the
// object if health runs out
///////////////////////////////////////////////////////////
public void Damage(float damage)
{
if (!enabled || !gameObject.active)
return;
m_CurrentHealth -= damage;
if (m_CurrentHealth <= 0.0f)
{
// TIP: if you want to display an effect on the object just
// before it dies - such as putting a crack in an armor the
// second before it shatters - this is the place
vp_Timer.In(Random.Range(MinDeathDelay, MaxDeathDelay), Die);
return;
}
// TIP: if you want to do things like play a special impact
// sound upon every hit (but only if the object survives)
// this is the place
}
///////////////////////////////////////////////////////////
// removes the object, plays the death effect and schedules
// a respawn if enabled, otherwise destroys the object
///////////////////////////////////////////////////////////
public void Die()
{
if (!enabled || !gameObject.active)
return;
RemoveBulletHoles();
gameObject.SetActiveRecursively(false);
if (DeathEffect != null)
Object.Instantiate(DeathEffect, transform.position, transform.rotation);
if (Respawns)
vp_Timer.In(Random.Range(MinRespawnTime, MaxRespawnTime), Respawn);
else
Object.Destroy(gameObject);
}
///////////////////////////////////////////////////////////
// respawns the object if no other object is occupying the
// respawn area. otherwise reschedules respawning
///////////////////////////////////////////////////////////
protected void Respawn()
{
// return if the object has been destroyed (for example
// as a result of loading a new level while it was gone)
if (this == null)
return;
// don't respawn if player is within checkradius
// TIP: this can be expanded upon to check for additional object layers
if (Physics.CheckSphere(m_StartPosition, RespawnCheckRadius, ((1 << vp_Layer.Player) | (1 << vp_Layer.Props))))
{
// attempt to respawn again until the checkradius is clear
vp_Timer.In(Random.Range(MinRespawnTime, MaxRespawnTime), Respawn);
return;
}
// reset health, position, angle and motion
m_CurrentHealth = Health;
transform.position = m_StartPosition;
transform.rotation = m_StartRotation;
if (rigidbody != null)
{
rigidbody.angularVelocity = Vector3.zero;
rigidbody.velocity = Vector3.zero;
}
// reactivate object and play spawn sound
gameObject.active = true;
if (audio != null)
audio.PlayOneShot(RespawnSound);
}
///////////////////////////////////////////////////////////
// removes any bullet decals currently childed to this object
///////////////////////////////////////////////////////////
protected void RemoveBulletHoles()
{
foreach (Transform t in transform)
{
Component[] c;
c = t.GetComponents();
if (c.Length != 0)
Object.Destroy(t.gameObject);
}
}
}
Heres the code im using, the parent respawns but the children do not :( (script is c#)
Your answer
Follow this Question
Related Questions
Multiple Cars not working 1 Answer
Can't set timer 1 Answer
SendMessage setName has no receiver! 1 Answer
How to spawn objects in a specific range of random location 1 Answer