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 gamedevsmith · Feb 23, 2014 at 01:53 PM · erroraudioraycastingwalkingfootsteps

Varied footsteps - Object reference not set to an instance of an object?

Hello! My aim is to have different footstep sounds depending on the ground the player is walking on. The over all structure is this: variable to determine if the player is walking, raycast down if player is walking to determine what ground he/she is on, play an audio clip depending on the tag.

I have this script so far, but I get the error "Object reference not set to an instance of an object?".. What is the problem here? I am not very experienced with raycasting unfortunately, haha.

 var snow : AudioClip;
 var metal : AudioClip;
 var walking : boolean;
 
 var hit : RaycastHit;
 var rayHit : boolean;
  
 function Update()
 {
     var dwn = transform.TransformDirection (-Vector3.up);
       if(Input.GetKeyDown("w") || Input.GetKeyDown("a") || Input.GetKeyDown("s") || Input.GetKeyDown("d"))
         {
            walking = true;
         }
 
         else if (Input.GetKeyUp("w") || Input.GetKeyUp("a") || Input.GetKeyUp("s") || Input.GetKeyUp("d"))
         {
               walking = false;
         }        
       
       if (walking&&Physics.Raycast (transform.position, dwn)&&hit.transform.gameObject.tag == "metalfloor") 
         {
            audio.clip = metal;
            audio.Play();
         }        
         
         if (walking&&Physics.Raycast (transform.position, dwn)&&hit.transform.gameObject.tag == "snowfloor") 
         {
            audio.clip = snow;
            audio.Play();
         }        
 
 }

Thanks for your time, you guys are great with questions :)

Comment
Add comment · Show 3
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 KiraSensei · Feb 23, 2014 at 01:57 PM 0
Share

You need to tell us at what line is the error. When you have an error, there usually is the line number with it, it helps a lot...

Whit a quick look at your code, I think several things can raise up this error :

  • hit is not valuated,

  • there is no audio source attached to the game object that contains this script

avatar image gamedevsmith · Feb 23, 2014 at 02:13 PM 0
Share

@$$anonymous$$iraSensei Thanks for your reply :), It is on line 21.

avatar image Bunny83 · Feb 23, 2014 at 03:04 PM 0
Share

What the... If you think this question is answered, select the answer which answers the question. If no answer answers your question but you've found a solution, post your own answer and accept this.

If you think this question can't be answered, is outdated, just a duplicate question, either delete the question or ask to close the question.

You should never delete just the content of the question which would render this question completely useless. This is a knowledge-base for all Unity users.

2 Replies

· Add your reply
  • Sort: 
avatar image
0

Answer by KiraSensei · Feb 23, 2014 at 02:22 PM

So "hit" should be the problem. You declared "hit" but you didn't use it when calling Physics.Raycast, so "hit" does not contain anything.

Try :

 if (walking&&Physics.Raycast (transform.position, dwn, hit)&&hit.transform.gameObject.tag == "metalfloor")

Same for the "snowfloor" tag.

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 KiraSensei · Feb 23, 2014 at 02:33 PM 1
Share

If it corrected your problem, please accept the answer by checking the tick box on the left of the answer.

avatar image
0

Answer by Nick4 · Feb 23, 2014 at 02:25 PM

 if (walking&&Physics.Raycast (transform.position, dwn)&&hit.transform.gameObject.tag == "metalfloor") 
 {
     audio.clip = metal;
     audio.Play();
 }

You haven't used the variable hit as a parameter in Physics.Raycast(). I think this will fix your problem :

     if (walking && Physics.Raycast (transform.position, dwn, hit)) 
     {
         if(hit.transform.gameObject.tag = "metalfloor")
         {
             audio.clip = metal;
             audio.Play();
         }
     }
 
     if (walking && Physics.Raycast (transform.position, dwn, hit)) 
     {
         if(hit.transform.gameObject.tag = "snowfloor")
         {
             audio.clip = snow;
             audio.Play();
         }
     }
Comment
Add comment · 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

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

Footstep Audio Not Working? 1 Answer

Footsteps Script for Running and Walking 3 Answers

Play different sounds on different surfaces? 1 Answer

Random footsteps 5 Answers

Footstep Sound Error toSample > fromSample 1 Answer


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