Question by
Banelisku · Sep 11, 2017 at 06:06 AM ·
audioaudiosourceaudioclip
Object Reference not set to an instance of an object
Hey guys I am trying to play an audio clip as soon as my character passes a position of an instantiated object that later gets destroyed. If anyone could help me out. Here is the code
public class TerrainGenerator : MonoBehaviour {
public GameObject[] Obstacle;
List<GameObject> objs = new List<GameObject>();
List<Vector3> obsPos = new List<Vector3>();
private int gapHeight = 430;
private int distanceInt = 0;
private int spacer = 100;
private float minSpawn = -160;
private float maxSpawn = 80;
private float botEdge = -74;
private AudioSource audioSource;
public AudioClip getPointSound;
// Use this for initialization
void Start () {
for (int j = 0; j < 5; j++)
{
float randFloat = Random.Range(minSpawn, maxSpawn);
for (int i = 0; i < Obstacle.Length; i++)
{
if (i == 0)
{
var obj = Instantiate(Obstacle[i], new Vector3(CameraController.cameraPos.x + 30 + distanceInt, botEdge + randFloat, 0), Quaternion.identity);
obsPos.Add(new Vector3(CameraController.cameraPos.x + 30 + distanceInt, -74 + randFloat, 0));
objs.Add(obj);
}
else
{
var obj = Instantiate(Obstacle[i], new Vector3(CameraController.cameraPos.x + 30 + distanceInt, botEdge + randFloat + gapHeight, 0), Quaternion.identity);
objs.Add(obj);
}
}
distanceInt += spacer;
}
distanceInt -= spacer;
}
// Update is called once per frame
void Update () {
if (BirdMovement.BirdAlive)
{
if (CameraController.cameraPos.x > 1)
{
if (CameraController.cameraPos.x % spacer == 0)
{
//Debug.Log("Created Object");
float randFloat = Random.Range(minSpawn, maxSpawn);
for (int i = 0; i < Obstacle.Length; i++)
{
if (i == 0)
{
var obj = Instantiate(Obstacle[i], new Vector3(CameraController.cameraPos.x + 30 + distanceInt, botEdge + randFloat, 0), Quaternion.identity);
objs.Add(obj);
obsPos.Add(new Vector3(CameraController.cameraPos.x + 30 + distanceInt, -74 + randFloat, 0));
//Debug.Log(obj.transform.position);
}
else
{
var obj = Instantiate(Obstacle[i], new Vector3(CameraController.cameraPos.x + 30 + distanceInt, botEdge + randFloat + gapHeight, 0), Quaternion.identity);
objs.Add(obj);
}
}
}
}
// Removing gameobjects after the camera has gone past them
foreach (GameObject go in objs.ToArray())
{
if (go.transform.position.x < CameraController.cameraPos.x - 300)
{
Destroy(go);
objs.Remove(go);
}
}
Vector3 pos = BirdMovement.BirdPosition.transform.position;
foreach(Vector3 obstaclePos in obsPos.ToArray())
{
if(pos.x > obstaclePos.x)
{
audioSource.clip = getPointSound;
audioSource.Play();
BirdMovement.scoreCount++;
Debug.Log(BirdMovement.scoreCount);
obsPos.Remove(obstaclePos);
// Not sure why this line is not working but now is
GameObject.Find("Conch").GetComponent<TextMesh>().text = BirdMovement.scoreCount.ToString();
}
}
}
}
}
Comment
You haven't set`private AudioSource audioSource;`
put this in Start()
-> audioSource = GetComponent<AudioSource>();
If AudioSource is attached to the same object as the script. or set it public and and do it in editor.