- Home /
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 text
$$anonymous$$ake sure your AdudioSource
is not destroyed at runtime. Post code how you are playing sound.
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 ();
}
}
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.
haha it works!! and the audio problem is also gone! thank you so much for the help
hahah i knew that. that's why i want to see your script
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.
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
Follow this Question
Related Questions
Space Shooter tutorial shot velocity issues 0 Answers
There is no 'AudioSource' attached to the "Cube" game object, but a script is trying to access it. 0 Answers
AudioSource and AudioClip. Stop and play drops an error. 1 Answer
MissingComponentException: no AudioClip even if i add it 1 Answer
Creating a spread shot using prefabs 0 Answers