- Home /
Destroy() problem
Hey,
I have a problem with the Destroy() function.
if (currentHealth == 0)
{
CharMovement.enabled = false;
follCam.enabled = false;
Destroy(gameObject,0.5f);
pSpawn.SendMessage("respawn");
}
I'm using this if statement, if the player dies. But those 3 lines except Destroy() doesn't take effect. I tried it with putting follCam.enabled above the if statement and it worked >.<
What I'm doing wrong !?
Regards,
LifeArtist
What are Char$$anonymous$$ovement and follCam? The way you're using them, they should be references to scripts attached to this same gameobject which you created using GetComponent.
Wait, maybe I read that wrong. Are you saying that everything inside the if statement is working EXCEPT the Destroy()? Or the other way around?
The other way round :P but when i put it above the if it work :O
void Start()
{
plSpawn = GameObject.FindGameObjectWithTag("PlayerSpawn");
mainCamera = GameObject.FindGameObjectWithTag("$$anonymous$$ainCamera");
Char$$anonymous$$ovement = GetComponent<char$$anonymous$$ovement>();
follCam = mainCamera.GetComponent<FollowingCamera>();
pSpawn = plSpawn.GetComponent<playerSpawn>();
follCam.enabled = true;
}
this is the way how I get the components
You're using the wrong function to find PlayerSpawn and $$anonymous$$ainCamera by their tag. It should be GameObject.FindWithTag. If you're finding multiple game objects, like to store them in an array, then you'd use GameObject.FindGameObjectsWithTag.
Answer by sumeetkhobare · May 02, 2014 at 06:46 PM
why Don't you write a debug log function in the 'if' condition.
for every statement. Like,
Debug.Log("charMovement:"+charMovement.enabled);
Debug.Log("follCam:"+follCam.enabled);
I am sure that the code is working fine.. But as you said that its called by enemy and i assume there are many enemies in your scene.. There's a possibility that somewhere in some other attached scripts your components are getting enabled again.
WHere's this code written:
void Start()
{
plSpawn = GameObject.FindGameObjectWithTag("PlayerSpawn");
mainCamera = GameObject.FindGameObjectWithTag("MainCamera");
CharMovement = GetComponent<charMovement>();
follCam = mainCamera.GetComponent<FollowingCamera>();
pSpawn = plSpawn.GetComponent<playerSpawn>();
follCam.enabled = true;
}
If it's written in a enemy script and you are continuously making new enemies, then it can be a reason to set the follCam to enabled again.
$$anonymous$$h this is the class attached to the player:
using UnityEngine;
using System.Collections;
public class CharHealth : $$anonymous$$onoBehaviour
{
public SpriteRenderer charSpriteRenderer;
public GameObject mainCamera;
public GameObject plSpawn;
public FollowingCamera follCam;
public PlayerSpawn pSpawn;
public Char$$anonymous$$ovement char$$anonymous$$ovement;
public int currentHealth = 100;
public float DamageEffectPause = 0.5f;
public int blinkAmount = 3;
public bool canBeDamaged = true;
void Start()
{
plSpawn = GameObject.FindWithTag("PlayerSpawn");
mainCamera = GameObject.FindWithTag("$$anonymous$$ainCamera");
char$$anonymous$$ovement = GetComponent<Char$$anonymous$$ovement>();
follCam = mainCamera.GetComponent<FollowingCamera>();
pSpawn = plSpawn.GetComponent<PlayerSpawn>();
follCam.enabled = true;
}
void applyDamage(int damage)
{
if (currentHealth > 0 && canBeDamaged == true)
{
currentHealth -= damage;
if (currentHealth < 0)
{
currentHealth = 0;
}
if (currentHealth == 0)
{
char$$anonymous$$ovement.enabled = false;
follCam.enabled = false;
Destroy(gameObject);
pSpawn.Send$$anonymous$$essage("respawn");
}
else
{
StartCoroutine(DamageEffect());
}
}
}
IEnumerator DamageEffect ()
{
canBeDamaged = false;
charSpriteRenderer.enabled = false;
yield return new WaitForSeconds(0.1f);
charSpriteRenderer.enabled = true;
yield return new WaitForSeconds(0.1f);
charSpriteRenderer.enabled = false;
yield return new WaitForSeconds(0.1f);
charSpriteRenderer.enabled = true;
yield return new WaitForSeconds(0.1f);
charSpriteRenderer.enabled = false;
yield return new WaitForSeconds(0.1f);
charSpriteRenderer.enabled = true;
canBeDamaged = true;
}
}
$$anonymous$$y idea: I have a char Prefab. I spawn one and turn on the cam(folCam). If the "Prefab" dies he should disable char$$anonymous$$ove .. and follCam.. Or I get a error that they want to access my Transform(Prefab) which doesnt exists.
But this isn't working.
Here the errors:
Number 1:
$$anonymous$$issingReferenceException: The object of type 'Transform' has been destroyed but you are still trying to access it.
Your script should either check if it is null or you should not destroy the object.
UnityEngine.Transform.get_position () (at C:/BuildAgent/work/d3d49558e4d408f4/artifacts/EditorGenerated/UnityEngineTransform.cs:27)
Char$$anonymous$$ovement.FixedUpdate () (at Assets/Scripts/Character/Char$$anonymous$$ovement.cs:38)
Number 2:
$$anonymous$$issingReferenceException: The object of type 'Transform' has been destroyed but you are still trying to access it.
Your script should either check if it is null or you should not destroy the object.
UnityEngine.Transform.get_position () (at C:/BuildAgent/work/d3d49558e4d408f4/artifacts/EditorGenerated/UnityEngineTransform.cs:27)
FollowingCamera.CheckX$$anonymous$$argin () (at Assets/Scripts/FollowingCamera.cs:19)
FollowingCamera.TrackPlayer () (at Assets/Scripts/FollowingCamera.cs:43)
FollowingCamera.FixedUpdate () (at Assets/Scripts/FollowingCamera.cs:32)
I thought I can prevent those errors by disabling the 2 scripts.
Still can't find the problem in my code. $$anonymous$$aybe I do something wrong with Destroy()