Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 14 Next capture
2021 2022 2023
2 captures
12 Jun 22 - 14 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 eminem1984 · Jul 20, 2017 at 06:29 AM · audiosourcespace shooterenemy spawnshotmissingcomponentexception

Space Shooter - Error says "There is no Audio Source attached to the ememy ship GameObject" which i actually have done

Hey guys,

When I tried to load the "Enemy Maneuver" script in the GameObject of "Enemy Ship", it failed to be loaded as the console saying I haven't added a Audio Source to the game object of "Enemy Ship", but the problem is, this has been done in previous step already- as you can see in the below picture, i've attached "Bolt Enemy" into "Shot" in the inspector, and i've checked several times that the audio source of "Enemy weapon" is well attached in "Bolt Enemy". More importantly, it ran okay before (i've tested it) and it started to pop out this problem when the script of "Enemy Maneuver" tends to be loaded.

Any ideas do you reckon the problem might be? thanks a lot. I am running Unity v5.6.1f1 on WIN7 PC.
alt textalt text

enemy-ship-inspector.png (88.9 kB)
error-info.png (42.2 kB)
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 vir1234 · Jul 20, 2017 at 06:33 AM 0
Share

post your Enemy$$anonymous$$aneuver Script

avatar image SohailBukhari · Jul 20, 2017 at 06:37 AM 0
Share

$$anonymous$$ake sure your AdudioSource is not destroyed at runtime. Post code how you are playing sound.

avatar image eminem1984 · Jul 20, 2017 at 06:55 AM 0
Share

I've attached the two section of codes here - former one is the Enemy$$anonymous$$aneuver, latter one is how the audio played. Thanks.

using System.Collections; using System.Collections.Generic; using UnityEngine;

public class $$anonymous$$ovement_Enemy : $$anonymous$$onoBehaviour {

 private float new$$anonymous$$aneuver;
 private float target$$anonymous$$aneuver;

 public Vector2 Start_wait;
 public Vector2 maneuverTime;
 public Vector2 maneuverWait;
 public float swing;  
 public float speed;
 public Rigidbody rb;
 public Boundary b;
 public float tilt;
 public float x$$anonymous$$in, x$$anonymous$$ax, z$$anonymous$$in, z$$anonymous$$ax;

 private Vector3 currentSpeed; 


 void Start () {

     rb = GetComponent<Rigidbody> ();
     StartCoroutine (Evasion ());
     currentSpeed = rb.velocity;
 }


 IEnumerator Evasion () { 

     yield return new WaitForSeconds (Random.Range(Start_wait.x, Start_wait.y));

     while (true) {

         target$$anonymous$$aneuver = Random.Range (1, swing) * - $$anonymous$$athf.Sign(transform.position.x); 
         yield return new WaitForSeconds (Random.Range(maneuverTime.x, maneuverTime.y));
         target$$anonymous$$aneuver = 0;
         yield return new WaitForSeconds (Random.Range(maneuverWait.x, maneuverWait.y)); 
     
     }
 
 } 



 void FixedUpdate () {
     new$$anonymous$$aneuver = $$anonymous$$athf.$$anonymous$$oveTowards (rb.velocity.x, target$$anonymous$$aneuver, Time.deltaTime * speed); //current value move towards the target value 
     rb.velocity = new Vector3(new$$anonymous$$aneuver, 0.0f, currentSpeed.z); 
     rb.position = new Vector3($$anonymous$$athf.Clamp(rb.position.x, b.x$$anonymous$$in, b.x$$anonymous$$ax), 0.0f, $$anonymous$$athf.Clamp(rb.position.z, b.z$$anonymous$$in, b.z$$anonymous$$ax)); 
     rb.rotation = Quaternion.Euler(0.0f, 0.0f, rb.velocity.x * - tilt); 

     }

}


using System.Collections; using System.Collections.Generic; using UnityEngine;

public class Weapon_Enemy : $$anonymous$$onoBehaviour {

 private AudioSource audio;

 public Transform shotSpawn; 
 public GameObject shot;
 public float fireRate;
 public float nextFire; 

 void Start (){

     InvokeRepeating ("Fire", nextFire, fireRate); 
 
 }
 // Update is called once per frame
 void Fire () {

     Instantiate (shot, shotSpawn.position, shotSpawn.rotation);
     audio = GetComponent<AudioSource> ();
     audio.Play ();
 }

}

2 Replies

· Add your reply
  • Sort: 
avatar image
2
Best Answer

Answer by vir1234 · Jul 20, 2017 at 07:00 AM

your problem is your class name is movement _enemy and your file name is EnemyManuever. your file name and class name must be the same.

Comment
Add comment · Show 3 · 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 eminem1984 · Jul 20, 2017 at 07:05 AM 0
Share

oh i see...

avatar image eminem1984 · Jul 20, 2017 at 07:45 AM 0
Share

haha it works!! and the audio problem is also gone! thank you so much for the help

avatar image vir1234 eminem1984 · Jul 20, 2017 at 07:47 AM 0
Share

hahah i knew that. that's why i want to see your script

avatar image
1

Answer by FlaSh-G · Jul 20, 2017 at 06:52 AM

The error message says that there is no AudioSource attached to Enemy Ship, and in your text you say you attach one to the Bolt Enemy object that is referenced by your Enemy Ship. This is exactly where the problem is. Unity won't transitively do GetComponent. If you look for a component on object X, it has to be on object X and not object Y with Y being referenced on X.

I don't know how it's supposed to have run before but not with the configuration you described in your text.

Either way... you'd have to go through the chain of references to get to the GameObject that actually has your AudioSource attached. Like

 enemyShip.GetComponent<Weapon_Enemy>().shotSpawn.GetComponent<AudioSource>().Play();

As you can probably imagine, this is not something you want to have in your code.

To avoid this, you have to think about what you're trying to do on a semantic level and how you could improve that. Why is there a script on an object that wants to trigger an AudioSource that it not only doesn't directly know? And even worse, the object it does know doesn't directly know either? There usually is a way of remodelling your context in order to keep this from happening.

The simplest way would usually be to pass the reference of the spawned object to the script that actually needs it. But it's worth trying to come up with other solutions as well.

Comment
Add comment · Show 2 · 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 eminem1984 · Jul 20, 2017 at 07:03 AM 0
Share

okay, i gonna give it a go see what happens :)

avatar image eminem1984 · Jul 20, 2017 at 11:53 AM 0
Share

Yes you are correct, though the audio error disappeared a while after I fixed the problem in the Enemy$$anonymous$$aneuver Script, it came up again when enter into the Play mode. What you've written re$$anonymous$$ds me that the AudioSource is actually a component of the object "Bolt Enemy" attached on the GameObject "shot", as i used "shot" for the container, then i modified my code to :

  audio = shot.GetComponent<AudioSource> (); 

Now the component is correctly referenced. The error is gone finally!! thanks for the answer and advice.

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

72 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

Related Questions

Space Shooter tutorial shot velocity issues 0 Answers

AudioSource and AudioClip. Stop and play drops an error. 1 Answer

There is no 'AudioSource' attached to the "Cube" game object, but a script is trying to access it. 0 Answers

MissingComponentException: no AudioClip even if i add it 1 Answer

Creating a spread shot using prefabs 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