- Home /
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 :)
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
@$$anonymous$$iraSensei Thanks for your reply :), It is on line 21.
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.
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.
If it corrected your problem, please accept the answer by checking the tick box on the left of the answer.
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();
}
}
Your answer
Follow this Question
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