Get Component Script Error
Hello I am a Beginner at unity and I am having some trouble with errors
I am trying to create a jump scare but 4 errors come up and it has something to do with Get Component and I don't know how to fix it (The Code is in JavaScript)
My Code:
var scare : GameObject;
var played = false;
var trig = false;
var scareSound: AudioClip;
////////Make sure its not visable and reset////////
function Start () {
trig = false;
scare.renderer.enabled = false;
}
/////When player enters trigger/////// set to true///////
function OnTriggerEnter (other : Collider) {
trig = true;
}
//////Enable renderer and trigger sound and timer/////
function Update () {
if (trig == true) {
scare.renderer.enabled = true;
removeovertime ();
makehimscream ();
}
}
//// timer ////
function removeovertime () {
yield WaitForSeconds (0.8);
scare.renderer.enabled = false;
Destroy(this.gameObject);
}
//// sound /////
function makehimscream () {
if (!played) {
played = true;
audio.PlayOneShot(scareSound);
}
}
Here Are My Errors:
Assets/Trig.js(9,15): BCE0144: 'UnityEngine.GameObject.renderer' is obsolete. Property renderer has been deprecated. Use GetComponent() instead. (UnityUpgradable)
AND
Assets/Trig.js(18,23): BCE0144: 'UnityEngine.GameObject.renderer' is obsolete. Property renderer has been deprecated. Use GetComponent() instead. (UnityUpgradable)
AND
Assets/Trig.js(26,15): BCE0144: 'UnityEngine.GameObject.renderer' is obsolete. Property renderer has been deprecated. Use GetComponent() instead. (UnityUpgradable)
AND
Assets/Trig.js(34,17): BCE0144: 'UnityEngine.Component.audio' is obsolete. Property audio has been deprecated. Use GetComponent() instead. (UnityUpgradable)
Please Help Me.
Thanks in advance
Answer by Landern · Feb 09, 2017 at 06:28 PM
Those properties/members have been deprecated/marked as obsolete. The errors tell you what you need to do. GetComponent is needed to get the reference to the render an audio components that had a field/property in unity previously for ease of use.
modify your script as follows:
var scare : GameObject;
var played = false;
var trig = false;
var scareSound : AudioClip;
////////Make sure its not visable and reset////////
function Start() {
trig = false;
scare.GetComponent. < Renderer > ().enabled = false;
}
/////When player enters trigger/////// set to true///////
function OnTriggerEnter(other : Collider) {
trig = true;
}
//////Enable renderer and trigger sound and timer/////
function Update() {
if (trig == true) {
scare.GetComponent. < Renderer > ().enabled = true;
removeovertime();
makehimscream();
}
}
//// timer ////
function removeovertime() {
yield WaitForSeconds(0.8);
scare.GetComponent. < Renderer > ().enabled = false;
Destroy(this.gameObject);
}
//// sound /////
function makehimscream() {
if (!played) {
played = true;
GetComponent. < AudioSource > ().PlayOneShot(scareSound);
}
}