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 3 · Sep 07, 2013 at 11:05 PM · raycastfpsdestroyhitobject reference

Object Reference not set to an instance of an object only appears occasionally?

So I fire a raycast, which acts as a gun, and when it hits a bird it is supposed to explode and destroy the bird gameobject. So, here is my problem, while it always spawns the explosion and plays the sound, it doesn't always destroy the bird. And when it doesn't it leaves the all dreaded object reference not set error. This has completely stumped me, any ideas why?

I also fall through the terrain sometimes when I shoot a bird, the explosion and sound still happens, but the terrain just deletes, Im not sure if this is related or if the bird deletes when I do it, (and the terrain has the "dirt" tag)

Thanks, and here is my script and a picture of the bird gameobject inspector settings. alt text alt text

     var hit : RaycastHit;
 var dirtPrefab : GameObject;
 var birdexplode : GameObject;
 var muzzlePoint : Transform;
 var c1 : Color = Color.grey;
 var c2 : Color = Color.white;
 var bulletdistance = 200000;
 var lineRenderer : LineRenderer;
 function Start() { 
      lineRenderer = gameObject.AddComponent(LineRenderer);
      lineRenderer.material = new Material (Shader.Find("Particles/Additive"));
      lineRenderer.SetColors(c1, c2);
      lineRenderer.SetWidth(0.04,0.01);
      lineRenderer.SetVertexCount(2);
 }  
  
 var timer : float;
 var timeToWait : float = 0.1;
 function Update(){  
 timer += Time.deltaTime;
 if(timer > timeToWait)
 {
                var origin = transform.position;
 var direction = transform.forward;
 var endPoint = origin + direction * bulletdistance;
        lineRenderer.SetPosition(0, origin);
     if(Input.GetButtonDown("Fire1")){
        StartCoroutine("Shoot");
   //lineRenderer.SetVertexCount(2);
        audio.Play();
     }
 }
 }
 
 function Shoot(){
     var fwd = muzzlePoint.TransformDirection (Vector3.forward);
     if (Physics.Raycast (muzzlePoint.position, fwd, hit, bulletdistance)){
     timer = 0;
 endPoint = hit.point;
 lineRenderer.SetPosition(1, endPoint);
               if(hit.collider.gameObject.tag == "dirt"){
          Instantiate(dirtPrefab, hit.point, hit.transform.rotation); 
        //  Invoke("LineRenderClear", 1);
        }
                      if(hit.collider.gameObject.tag == "flyinganimal"){
               Invoke("destroyhit", 1.3);
        hit.transform.audio.Play();
        Debug.Log("YOU HIT DA BIRD");
  hit.transform.gameObject.GetComponent("Seagull").enabled = false;
                 Instantiate(birdexplode, hit.point, hit.transform.rotation); 
        }
     }
 } 
 
 function LineRenderClear () {
 // lineRenderer.SetVertexCount(0);
 }
 
 function destroyhit () {
 Destroy(hit.transform.gameObject);
 }


Thanks Again,

screen shot 2013-09-07 at 4.04.17 pm.png (38.4 kB)
screen shot 2013-09-07 at 4.23.04 pm.png (7.5 kB)
Comment
Add comment · Show 2
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 meat5000 ♦ · Sep 07, 2013 at 11:21 PM 0
Share

It looks to me like you should consider using the OnCollision functions to handle this.

Also, is there any particular reason why you use

 StartCoroutine("Shoot");
avatar image 3 · Sep 07, 2013 at 11:25 PM 0
Share

Where should I use "oncollision" and what should I replace it with?

And no, I was really tired when I made this script, i replaced it with Invoke, thanks. Doesn't seem to change much though, it does happen less often though, not sure if its a coincidence or not. What does it change?

I dont think i could use oncollision with a raycast, can I?

1 Reply

· Add your reply
  • Sort: 
avatar image
1

Answer by meat5000 · Sep 07, 2013 at 11:35 PM

 function OnCollisionEnter()
 {
 
 }
 
 function OnCollisionStay()
 {
 
 }
 
 function OnCollisionExit()
 {
 
 }

Basically, you pick one that is best suited for the situation.

I would try something like this, on a bird

 function OnCollisionEnter(col : Collision)
 {
     if(col.collider.gameObject.name == "Bullet")
     {
         Destroy(gameObject);
     }
 }

Also it looks like you are mixing code.

C# ?

 lineRenderer.material = new Material (Shader.Find("Particles/Additive"));

JS ?

 var dirtPrefab : GameObject;

Bad JS ?

 var origin = transform.position;

If you are using JS, declare your variables fully.

 var origin : Vector3 = transform.position;


Also, I'm not so sure that shoot function needs to be in a coroutine at all.

Comment
Add comment · Show 3 · 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 3 · Sep 07, 2013 at 11:42 PM 0
Share

again, thanks, but it's a raycast, unless there is something I don't know, a ray wont register as a collider.

As for the other things, thanks, it helped. But I still don't know why it occasionaly isn't working and how to fix it, any ideas?

avatar image meat5000 ♦ · Sep 08, 2013 at 10:49 AM 0
Share

Look again at the second half of the answer :P

Also, birdexplode is not being set to the object.

In the Start() routine your need to fill birdexplode with whatever the actual object is you are trying to instantiate.

You could also consider passing your hit info to the destroyhit() function as an argument.

Another point,

 Invoke("destroyhit", 1.3);

A lot can happen in 1.3 seconds. The information in 'hit' may have changed by the time the function is invoked.

avatar image meat5000 ♦ · Sep 08, 2013 at 09:33 PM 0
Share

Also I just noticed you are not Zeroing your timer, but adding to it.

 var timer : float;
 timer += Time.deltaTime;

make timer = 0 in start

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

16 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

Related Questions

Destroying object using his name and raycast 2 Answers

Help destroying gameObject on RayCastHit? 1 Answer

Hit distance Change light range 1 Answer

Do raycasts only collide with colliders or can I detect a hit through something else? 2 Answers

Particle Instantiation problem. 0 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