- Home /
Error Script Winning the Coconut Game (Surviving Island)
Sorry for the multiple questions but I'm a newbie u.u
It gaves me this error:
MissingComponentException: There is no 'AudioSource' attached to the "target" game object, but a script is trying to access it. You probably need to add a AudioSource to the game object "target". Or your script needs to check if the component is attached before using it. UnityEngine.AudioSource.PlayOneShot (UnityEngine.AudioClip clip) (at C:/BuildAgent/work/14194e8ce88cdf47/Runtime/ExportGenerated/Editor/BaseClass.cs:504) TargetCollision+$targetHit$1+$.MoveNext () (at Assets/Scripts/TargetCollision.js:13) UnityEngine.MonoBehaviour:StartCoroutine(String) TargetCollision:OnCollisionEnter(Collision) (at Assets/Scripts/TargetCollision.js:8)
Assets/Scripts/CoconutWin.js(16,1): BCE0043: Unexpected token: winCell.
The first one is related to audio clips but I don't understand why, I inserted all the sound clips that I need :S
The second one is what is giving me the problem :(
Here is the script:
#pragma strict
static var targets : int = 0;
static var haveWon : boolean = false;
var winSound : AudioClip;
var cellPrefab : GameObject;
function Start () {
}
function Update () {
if(targets==3 &&haveWon == false){
targets=0;
audio.PlayOneShot(winSound);
winCell : GameObject = transform.Find("powerCell").gameObject;
winCell.transform.Translate(-1,0,0);
Instantiate(cellPrefab, winCell.transform.position,
transform.rotation);
Destroy(winCell);
haveWon = true;
}
}
@script RequireComponent(AudioSource)
Thanks guys for helping me so much, without you guys I would be anything on this project.
1st question. Select gameObject in question and go to Components and add AudioSource. Sorry dont have time atm to read 2nd question.
Answer by Geo.Ego · Dec 28, 2012 at 03:36 PM
For the MissingComponentException, you need to make sure that the object you have attached your script to has an AudioSource component attached to it. In the Hierarchy window, click the object that the script is attached to, then go to Component > Audio > Audio Source in the top menu. This will attach the component and you should be good there.
For the unexpected token, you need to add "var" to the beginning of the declaration. So change this:
winCell : GameObject = transform.Find("powerCell").gameObject;
to this:
var winCell : GameObject = transform.Find("powerCell").gameObject;
It's strictly a syntactical issue.
could you post the code updated please because I didn't undersand very much the explanation
Thank u very much! And sorry for the inconveniences
No inconvenience. This is the place to go to learn, especially when you show, as you have, with written code that you've made an attempt in earnest and are hitting a wall. I updated my answer to show the exact code change. As far as adding the Audio Source, just follow the steps in the first paragraph and you'll be fine. If you still have trouble, tell me the name of the script and the name of the object you dragged it on to and I'll use those names to clarify further.
Your answer
Follow this Question
Related Questions
How to prevent an object moving when there is a collision with the hand controlled by Leap Motion? 0 Answers
detecting rigidbody collsion with no extra script 1 Answer
Help with VR Controller Collider 1 Answer
Colliding Animation 0 Answers
Pausing/Waiting/Stopping code in a 2D Collision Function 1 Answer