The question is answered, right answer was accepted
Audio Clip Error.
using UnityEngine;
using System.Collections;
public class Player : MonoBehaviour
{
public GameManager manager;
public float moveSpeed;
public GameObject deathParticles;
private float maxSpeed = 5f;
private Vector3 input;
private Vector3 spawn;
public AudioClip[] audioClip;
public Rigidbody myRigidbody { get; private set; }
// Use this for initialization
void Start ()
{
spawn = transform.position;
manager = manager.GetComponent<GameManager>();
this.myRigidbody = this.GetComponent<Rigidbody>();
}
void FixedUpdate ()
{
input = new Vector3(Input.GetAxisRaw ("Horizontal"), 0, Input.GetAxisRaw ("Vertical"));
if (myRigidbody.velocity.magnitude < maxSpeed)
{
myRigidbody.AddRelativeForce(input * moveSpeed);
}
if (transform.position.y < -2)
{
Die();
}
}
void OnCollisionEnter(Collision Other)
{
if (Other.transform.tag == "Enemy")
{
Die();
}
}
void OnTriggerEnter(Collider other)
{
if (other.transform.tag == "Enemy")
{
Die();
}
if (other.transform.tag == "Token")
{
manager.tokenCount += 1;
PlaySound(0);
Destroy(other.gameObject);
}
if (other.transform.tag == "Goal")
{
manager.CompleteLevel();
PlaySound(1);
}
}
void PlaySound(int clip)
{
audio.clip = audioClip[clip];
audio.Play();
}
void Die()
{
Instantiate(deathParticles, transform.position, Quaternion.Euler(270, 0, 0));
transform.position = spawn;
}
}
The Code Problem
void PlaySound(int clip) { audio.clip = audioClip[clip]; audio.Play(); } "audio.clip" and "audio.Play();"
Unity Error;
Error 1 'UnityEngine.Component' does not contain a definition for 'clip' and no extension method 'clip' accepting a first argument of type 'UnityEngine.Component' could be found (are you missing a using directive or an assembly reference?)
Error 2 'UnityEngine.Component' does not contain a definition for 'Play' and no extension method 'Play' accepting a first argument of type 'UnityEngine.Component' could be found (are you missing a using directive or an assembly reference?)
Help me Pls :(
Answer by HenryStrattonFW · Feb 04, 2017 at 11:03 PM
Error 1 is a simple type casting issue. A lot of the old access properties (like audio, rigidbody, colider, etc) are no longer stored as their type, but as their base type "Component" this I believe was related to unity making their code base a bit more modular.
This is also why these properties are marked as "Deprecated" and should be phased out of use. I would advise using GetComponent to get the AudioSource component. like the following example.
private AudioSource audioSource;
void Awake()
{
audioSource = GetComponent<AudioSource>();
}
Then play your audio through "audioSource" instead of "audio"
Error 2 is the same issue, since "audio" is type "Component" it does not have a method "Play" but when you swap to using an explicitly typed variable for your audioSource this should not be a problem.
I assure you the logic provided is correct in the context of the issue it was given to address. If you're having problems with it can you provide some specifics as to how it is not working in your given situation? With more specifics I may be able to suggest potential fixes.
Does this fix also work with transform, because am getting the same exact error when I use transform.length
I created a variable of spawn point of type transform with the [] , following a brackeys dodge block tutorial but its not working like it is on the tutorial am getting those 2 errors
UnityEngine.transform' does not contain a definition for 'length' and no extension method 'length' accepting a first argument of type 'UnityEngine.transform' could be found (are you missing a using directive or an assembly reference?)
The error there is saying that transform does not have a property or method 'length' which is completely true, you are trying to access a transform (singular) where it seems you're intending to access a transform array (an array does has a .length property).
Double check the variable you're accessing, chances are you're targeting the wrong variable, or have defined the variable as a transform ("Transform") ins$$anonymous$$d of an array ("Transform[]")
Answer by hossenshovon1 · Apr 23, 2018 at 07:49 PM
This logic is not supporting , please say what to do
Does this fix also work with transform, because am getting the same exact error when I use transform.length
I created a variable of spawn point of type transform with the [] , following a brackeys dodge block tutorial but its not working like it is on the tutorial am getting those 2 errors
UnityEngine.transform' does not contain a definition for 'length' and no extension method 'length' accepting a first argument of type 'UnityEngine.transform' could be found (are you missing a using directive or an assembly reference?)
I can't do audio. Can someone teach me =(((((
Follow this Question
Related Questions
How to reduce delay when playing sound 0 Answers
Pick Up Sound 0 Answers
Why won't my second Audio Source play? 0 Answers
Audio won't play, no matter what code or method I try 0 Answers
How to mute multi-able audioclips within a single audiosource? 0 Answers