Jump to content
This site uses cookies! Learn More

This site uses cookies!

armedunity.com uses cookies to improve user experience.

By continuing to use this site, you agree to allow us to store cookies on your computer.

Question

So i am new to unity and i found this FPS-Kit to be really usefull. Sadly i runned into a strange bug that i got rid of but i do not know why.

 

I added some Zombies into the Kit. Then i added Colliders to all bullets to make the Zombies die when get hit.

 

My ZombieScript:

 

-------------------------------------------------------------------------------------------------------

 

var target: Transform;
  var health:float = 90;
  var navComponent: UnityEngine.AI.NavMeshAgent;
  var anim: Animator;
  var followDistance: float; // the distance in which to follow the player
  var minDistance: float;
var attackDistance: float;
  var deadd: int = 0;
  private var healthScript: HealthScript;


  function Start() {

      target = GameObject.FindWithTag("Player").transform;
      navComponent = this.transform.GetComponent(UnityEngine.AI.NavMeshAgent);
      anim = GetComponent("Animator");
      anim.SetBool("Attack", false);
  }
  
  function Update() {
      
     
     if (deadd == 0){
      if (target) {
          var distanceToTarget = Vector3.Distance(transform.position, target.transform.position);
           

          if (distanceToTarget <= followDistance) {
              if (distanceToTarget <= attackDistance) {
                  anim.SetBool("Attack", true);
              } else {
                  anim.SetBool("Attack", false);
              }
           if (distanceToTarget >= minDistance) {

           anim.SetBool("Stay", false);
                 
              navComponent.Resume();
              navComponent.SetDestination(target.position);
              anim.SetFloat("speed", 1);

                 }else {

           
                navComponent.Stop();
        
             
                }


         

        
       } else {
       anim.SetBool("Stay", true);
       navComponent.Stop();
       }
           

        }
        }
 }
function OnTriggerEnter  (col: Collider) {
    if (col.gameObject.tag == "Projectile") {
        deadd = 69;
        navComponent.Stop();
        yield WaitForSeconds(0.1);
        anim.SetBool("Die", true);
        yield WaitForSeconds(1.12);

        Destroy(gameObject);

    }
    if (col.gameObject.tag == "Player") {
        healthScript = gameObject.Find("Player").GetComponent(HealthScript);
        healthScript.Die();
        
    }
}

 

--------------------------------------------------------------------------------------------------------

 

Working fine like 90% of the time. But sometimes after i hit a Zombie the game "Pauses" and i get the following error:

 

MissingReferenceException: The object of type 'SphereCollider' 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.
ZombieBH+$OnTriggerEnter$69+$.MoveNext () (at Assets/ZombieBH.js:74)
UnityEngine.SetupCoroutine.InvokeMoveNext (IEnumerator enumerator, IntPtr returnValueAddress) (at C:/buildslave/unity/build/Runtime/Export/Coroutines.cs:17)

 

After that i can hit the Pause Button and the game goes on.

 

I only testet this on my m4. I don't use the other weapons in the game atm beside the rpg. Not sure if that happens on rpg too, could not reproduce it as fast as on the m4.

 

I added a "Sphere Collider" on my particle_body as as far as i know this is the part that hits the zombie first. I did not modyfiy the Destroy After Script. But i added colliders to a lot of things to test what exactly kills my zombie. Perhaps i forgot to remove a collider from one object but i could not find anything else with a collider beside particle_untagged.

 

--- I removed the collider from the particle_untagged and as for now the bug stopped to appear.

 

Was the Collider at particle_untagged the reason for my bug? And why? I didn't noticed that when i fire a weapon a particle_untagged appears in my Hierarchy. So i thought this does not have an impact?

 

I'd love to know if this was the reason for my Bug, and if i can improve my Zombie Script.

 

Sorry for my really bad englisch, i am from Germany.

 

 

Anyway, thank you 


 

Share this post


Link to post
Share on other sites

15 answers to this question

Recommended Posts


  • 1
Please login or register to see this quote.

 

The error is misleading for the script provided. Destroying the object cancels any other call after that so it shouldn't be throwing an error at all actually. The issue is still what you stated, but I believe it's showing the wrong line for the cause of it.

 

i believe the cause is your yield statements. If you yield for 2 seconds, it's going to continue to collect trigger calls in the meantime. You should remove any yield statements or at the start of OnTriggerEnter, put if(deadd == 69) return;

Share this post


Link to post
Share on other sites
  • 0

You're trying to access a GameObject's component after you already destroyed that GameObject. Thats what the error means, it's basically just a matter of re-arranging your code so that you still access the components you need before you destroy the GameObject.

Share this post


Link to post
Share on other sites
  • 0

Oh and i want to improve my RPG but i don't know how:

Atm i have my SphereCollider on the Explosion Rocket. This works fine as it kills any zombie in a good range. But the collider stays longer than the exlosion lasts, so zombies running into the sphere are dieing also, what is pretty dumb. Any way to fix this?

Same on the m4. The collider stays for like 2 seconds. so a zombie stading just after the one i hit runs into the remaining particle and dies.

 

In order to try to fix this i changed the Destroy after time from 2 to 1. Now the bug appears on the m4 again. 

 

I have no idea what happens atm.

Share this post


Link to post
Share on other sites
  • 0
Please login or register to see this quote.

I got that. But i do not know what accesses what. That's the point here.

Share this post


Link to post
Share on other sites
  • 0
Please login or register to see this quote.

Thank you, i solved it like 10 minutes ago; 

 

here is how i did it:

var target: Transform;
  var health:float = 90;
  var navComponent: UnityEngine.AI.NavMeshAgent;
  var anim: Animator;
  var followDistance: float; // the distance in which to follow the player
  var minDistance: float;
var attackDistance: float;
  var deadd: int = 0;
  private var healthScript: HealthScript;
  private var scoreManager: ScoreManager;


  function Start() {

      target = GameObject.FindWithTag("Player").transform;
      navComponent = this.transform.GetComponent(UnityEngine.AI.NavMeshAgent);
      anim = GetComponent("Animator");
      anim.SetBool("Attack", false);
  }
  
  function Update() {
      
     
     if (deadd == 0){
      if (target) {
          var distanceToTarget = Vector3.Distance(transform.position, target.transform.position);
           

          if (distanceToTarget <= followDistance) {
              if (distanceToTarget <= attackDistance) {
                  anim.SetBool("Attack", true);
              } else {
                  anim.SetBool("Attack", false);
              }
           if (distanceToTarget >= minDistance) {

           anim.SetBool("Stay", false);
                 
              navComponent.Resume();
              navComponent.SetDestination(target.position);
              

                 }else {

           
                navComponent.Stop();
        
             
                }


         

        
       } else {
       anim.SetBool("Stay", true);
       navComponent.Stop();
       }
           

        }
        }
 }
function OnTriggerEnter  (col: Collider) {
    if (col.gameObject.tag == "Projectile") {
        scoreManager = gameObject.Find("ScoreManager").GetComponent(ScoreManager);
        scoreManager.addScore(1);
        deadd = 69;
        navComponent.Stop();
        Destroy(GetComponent(BoxCollider));
        Destroy(GetComponent(AudioSource));
        Destroy(GetComponent(Rigidbody));

        yield WaitForSeconds(0.1);
        anim.SetBool("Die", true);
        yield WaitForSeconds(1.12);

        Destroy(gameObject);

    }
    if (col.gameObject.tag == "Player") {
        healthScript = gameObject.Find("Player").GetComponent(HealthScript);
        healthScript.Die();
        
    }
}

 

Sadly i run into another problem:

 

 

Initialize engine version: 5.6.0f3 (497a0f351392)
GfxDevice: creating device client; threaded=1
Direct3D:
    Version:  Direct3D 11.0 [level 11.0]
    Renderer: NVIDIA GeForce GT 530 (ID=0xde5)
    Vendor:   NVIDIA
    VRAM:     979 MB
    Driver:   22.21.13.8165
Begin MonoManager ReloadAssembly
Platform assembly: C:\Users\Julian\Documents\Meine Spiele\xZombie_Data\Managed\UnityEngine.dll (this message is harmless)
Loading C:\Users\Julian\Documents\Meine Spiele\xZombie_Data\Managed\UnityEngine.dll into Unity Child Domain
Platform assembly: C:\Users\Julian\Documents\Meine Spiele\xZombie_Data\Managed\Assembly-CSharp-firstpass.dll (this message is harmless)
Loading C:\Users\Julian\Documents\Meine Spiele\xZombie_Data\Managed\Assembly-CSharp-firstpass.dll into Unity Child Domain
Platform assembly: C:\Users\Julian\Documents\Meine Spiele\xZombie_Data\Managed\Assembly-CSharp.dll (this message is harmless)
Loading C:\Users\Julian\Documents\Meine Spiele\xZombie_Data\Managed\Assembly-CSharp.dll into Unity Child Domain
Platform assembly: C:\Users\Julian\Documents\Meine Spiele\xZombie_Data\Managed\Assembly-UnityScript-firstpass.dll (this message is harmless)
Loading C:\Users\Julian\Documents\Meine Spiele\xZombie_Data\Managed\Assembly-UnityScript-firstpass.dll into Unity Child Domain
Platform assembly: C:\Users\Julian\Documents\Meine Spiele\xZombie_Data\Managed\Assembly-UnityScript.dll (this message is harmless)
Loading C:\Users\Julian\Documents\Meine Spiele\xZombie_Data\Managed\Assembly-UnityScript.dll into Unity Child Domain
Platform assembly: C:\Users\Julian\Documents\Meine Spiele\xZombie_Data\Managed\UnityEngine.UI.dll (this message is harmless)
Loading C:\Users\Julian\Documents\Meine Spiele\xZombie_Data\Managed\UnityEngine.UI.dll into Unity Child Domain
Platform assembly: C:\Users\Julian\Documents\Meine Spiele\xZombie_Data\Managed\UnityEngine.Networking.dll (this message is harmless)

 

Loading C:\Users\Julian\Documents\Meine Spiele\xZombie_Data\Managed\UnityEngine.Networking.dll into Unity Child Domain
- Completed reload, in  0.181 seconds
Platform assembly: C:\Users\Julian\Documents\Meine Spiele\xZombie_Data\Managed\System.Core.dll (this message is harmless)
Platform assembly: C:\Users\Julian\Documents\Meine Spiele\xZombie_Data\Managed\System.dll (this message is harmless)
desktop: 1920x1080 60Hz; virtual: 3600x1149 at 0,0
<RI> Initializing input.
<RI> Input initialized.
<RI> Initialized touch support.
UnloadTime: 1.053339 ms
Platform assembly: C:\Users\Julian\Documents\Meine Spiele\xZombie_Data\Managed\UnityScript.Lang.dll (this message is harmless)
Platform assembly: C:\Users\Julian\Documents\Meine Spiele\xZombie_Data\Managed\Boo.Lang.dll (this message is harmless)
Internal: JobTempAlloc has allocations that are more than 4 frames old - this is not allowed and likely a leak
 
(Filename:  Line: 371)

Internal: JobTempAlloc has allocations that are more than 4 frames old - this is not allowed and likely a leak
 
(Filename:  Line: 371)

Internal: JobTempAlloc has allocations that are more than 4 frames old - this is not allowed and likely a leak
 
(Filename:  Line: 371)

Internal: JobTempAlloc has allocations that are more than 4 frames old - this is not allowed and likely a leak
 
(Filename:  Line: 371)

Internal: JobTempAlloc has allocations that are more than 4 frames old - this is not allowed and likely a leak
 
(Filename:  Line: 371)

 

 

this is my output_log

This happens every frame in my game, which slows everything down really bad. This has nothing to do with my problem above i think.

 

 

 

Share this post


Link to post
Share on other sites
  • 0

Ok now it gets interesting: I removed all of my trees. And it worked.

Then i used not the option "mass place trees". It worked, i had some trees.

When i used "mass place trees" the error showed up again. Help

Share this post


Link to post
Share on other sites
  • 0

Idk about that one, that might be a bug in your unity version. Idk tho bc I don't use unity terrain

Share this post


Link to post
Share on other sites
  • 0

What u use instead? So i may use that too, as i don't really like unity terrain anyway...
Is there also a way to place a lot of trees automatically?

 

Btw i use the Terrain Toolkit.

Share this post


Link to post
Share on other sites
  • 0

I build my own systems most of the time. And all mass place trees is is testing the contact point from the camera and randomly testing heights inside a a unit circle and placing them. You can even build that yourself in unity terrain if u wanted

Share this post


Link to post
Share on other sites
  • 0

ohw yea, btw any way to shorten this? Like do 10 times?

 

function Start() {
    Spawn();
    Spawn();
    Spawn();
    Spawn();
    Spawn();
    Spawn();
    Spawn();
    Spawn();
    Spawn();
    Spawn();
    Spawn();
    Spawn();
    Spawn();
    Spawn();
    Spawn();
    Spawn();
    Spawn();
    Spawn();
    Spawn();
  

    InvokeRepeating("Spawn", spawnTime, spawnTime);
}
 

Share this post


Link to post
Share on other sites
  • 0

Edit: My Bug 

(Filename:  Line: 371)

Internal: JobTempAlloc has allocations that are more than 4 frames old - this is not allowed and likely a leak
 

does not affect the game. Me "le dumbass" forgot that i runned my wallpaper engine in background. My cpu simpely can't handle both, the game and the animated wallpaper.

 

As it doesn't affect the game imma just going to live with it.

Share this post


Link to post
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Answer this question...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.


×
×
  • Create New...