OK, we are entering serious noob territory here so be fore warned. I want to be able to play different “walk” sounds based on the object that the player is colliding with.
I have a script containing variables attached to object 1, and I want to be able to read those variables and use them in object 2 when both objects are colliding.
I have the following in a script for object 1 (the floor object):
var materialType = "wood";
and I have the following in a script in object 2 (the Player):
var walkSound_default: AudioClip;
private var collisionOverlap=false;
private var playsound: AudioClip;
private var collObjectType="default";
function Update () {
if(collisionOverlap==true){
if (GetComponent(ThirdPersonController).moveSpeed>0.3){
playsound=walkSound_default;
if (collObjectType=="wood"){ playsound=collObject.gameObject.GetComponent(Sound_object_collision).walkSound;
}
PlayWalkSound(playsound, transform.position, 1);
}
collisionOverlap=false;
}
// CHECK FOR COLLISION
function OnCollisionStay(collision : Collision) {
if (collision.relativeVelocity.magnitude>3){
collisionOverlap=true;
collObjectType=collision.gameObject.GetComponent(Sound_object_collision).materialType;
}
}
//PLAY SOUND
function PlayWalkSound (clip : AudioClip, position : Vector3, volume : float)
{
if (!audio.isPlaying){
audio.clip = clip;
audio.volume = volume;
audio.Play();
}
}
There are two lines that give me a problem: the “playsound=” declaration in Update, and the “collObjectType=” line under the collision check.
Both of these lines give me NullReferenceExceptions.
This is a piece of code I’ve been staring at for a few days now. I’m giving up my ego and admitting that I just don’t have a clue. Anyone care to offer some advice?