Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by LifeArtist · May 01, 2014 at 09:18 PM · gameobjectdestroystatement

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

Comment
Add comment · Show 6
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image theredace · May 01, 2014 at 09:20 PM 0
Share

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.

avatar image theredace · May 01, 2014 at 09:46 PM 0
Share

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?

avatar image LifeArtist · May 01, 2014 at 09:55 PM 0
Share

The other way round :P but when i put it above the if it work :O

avatar image LifeArtist · May 01, 2014 at 09:57 PM 0
Share
     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

avatar image theredace · May 02, 2014 at 04:47 PM 0
Share

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.

Show more comments

2 Replies

· Add your reply
  • Sort: 
avatar image
0

Answer by akguldeniz · May 01, 2014 at 09:28 PM

Destroy(this.gameObject) may works

Comment
Add comment · Show 1 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image LifeArtist · May 01, 2014 at 09:56 PM 0
Share

didn't worked :(

avatar image
0

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.

Comment
Add comment · Show 4 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image LifeArtist · May 02, 2014 at 08:52 PM 0
Share

$$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;
     }
 }
avatar image theredace · May 02, 2014 at 09:13 PM 0
Share

I don't see anything wrong with that code.

avatar image LifeArtist · May 02, 2014 at 09:24 PM 0
Share

$$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.

avatar image LifeArtist · May 04, 2014 at 10:37 AM 0
Share

Still can't find the problem in my code. $$anonymous$$aybe I do something wrong with Destroy()

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

22 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

using Contains(gameObject) to find and destroy a gameObject from a list 2 Answers

Destroy object when transform.position.x > 10. 1 Answer

Destroy(gameObject) does not work 2 Answers

Placing a cube on the face of another 4 Answers

Collision Detection for a Prefab? 3 Answers


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges