Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 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 /
  • Help Room /
avatar image
0
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
Add comment · Show 1
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 Positive7 · Sep 11, 2017 at 06:16 AM 0
Share

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.

0 Replies

· Add your reply
  • Sort: 

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

129 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 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 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 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 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 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

Pick Up Sound 0 Answers

Trying to use derive pitch and db from realtime microphone input 0 Answers

I want the audio to play when the player nears the object, but fade away when they walk away. 0 Answers

Audio Cutting Out Unexplainably 1 Answer

Audio not playing when conditions are met. 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