indiegamedev

Unity3D – warning Messages – BCW0028: WARNING: Implicit downcast from ‘UnityEngine.Component’ to ‘UnityEngine.AudioSource’

Unity3D – warning Messages – BCW0028: WARNING: Implicit downcast from ‘UnityEngine.Component’ to ‘UnityEngine.AudioSource’

Implicit Downcast from… to…

BCW0028: WARNING: Implicit downcast from ‘UnityEngine.Component’ to ‘UnityEngine.AudioSource’

This script will generate the BCW0028 error:

...

// buttons[i] -> this is an Array of GameObject

...
// Assegno Audio INIZIO #############################################################################
function AssignAudio () {
  
	var audioSrc : AudioSource;
	
	for(var i : int = 0; i < buttons.Length; i++) // assegno a tutti gli oggetti nell'array l'audio
	    {
	      	// BCW0028: WARNING: Implicit downcast from 'UnityEngine.Component' to 'UnityEngine.AudioSource'.
	      	audioSrc = buttons[i].AddComponent ("AudioSource"); 
	      	

	      	
			buttons[i].audio.clip = buttonAudio0; // Add audio clip
			buttons[i].audio.playOnAwake = false; // not play on awake  
	    }

}// END AssignAudio()
// Assegno Audio FINE ###############################################################################
...

Solution: you should explicity cast the Components to AudioSource yourself using “as” like this:

Change the row:

audioSrc = buttons[i].AddComponent ("AudioSource");

with

audioSrc = buttons[i].AddComponent ("AudioSource") as AudioSource;
By |Unity3D, Video Games Development|Commenti disabilitati su Unity3D – warning Messages – BCW0028: WARNING: Implicit downcast from ‘UnityEngine.Component’ to ‘UnityEngine.AudioSource’

Unity3D – JS Advanced – extends System.Object

Unity3D – JS Advanced – extends System.Object

System.Object is the class from which (generally speaking) everything in Unity is derived.
Every single other thing we build in Unity, derives from System.Object.
You can use this to display variables in the inspector similar to how a Vector3 shows up in the inspector.

Attach to an Empty Object this:


class Test extends System.Object {
	var p = 5;
	var c = Color.white;
}
var test = Test ();

Inspect the Inspector!
Click the white triangle on the left of ‘Test’ to espand and see P and C var.

By |Unity3D, Video Games Development|Commenti disabilitati su Unity3D – JS Advanced – extends System.Object

Unity3D – Create a Class – Easy Example

Unity3D – Create a Class – Easy Example

Watch the code:


#pragma strict

class Audio // create a Class, you will have the tringle to expand Class content into Inspector
{
	var crunchSFX : AudioClip; // Assign in Inspector
}
var myAudioClass : Audio; // store the whole Class content inside a variable

 
function OnTriggerEnter(other : Collider) 
{
    // play the SFX calling Class
    audio.PlayOneShot(myAudioClass.crunchSFX, 1);
    
} // END OnTriggerEnter

The steps are:
1. Create a Class

class Audio 
{
	var crunchSFX : AudioClip; // Assign in Inspector
}

2. Store the whole Class content inside a variable

var myAudioClass : Audio;

3. Call the Class content using the statement:

//  variable with Class content . variable name
... myAudioClass.crunchSFX ...
By |Unity3D, Video Games Development|Commenti disabilitati su Unity3D – Create a Class – Easy Example

Unity3D – AddComponent – Javascript

Unity3D – AddComponent – Javascript

AddComponent to THIS GameObject

Create a Cube and attach:


#pragma strict

function Start() {
        // Adds the sphere collider to the game object
	var sc : SphereCollider;
	sc = gameObject.AddComponent ("SphereCollider");
	}
	
function Update(){
	}

Play and watch Cube params into Inspector

AddComponent to ANOTHER GameObject

Create a Cube

Create an EmptyObject and attach:


#pragma strict

private var cubeobj : GameObject;

function Start() {
	cubeobj = GameObject.Find("/Cube"); // Find gameobject with name

        // Adds the sphere collider to the game object
	var sc : SphereCollider;
	sc = cubeobj.AddComponent ("SphereCollider");
	}
	
function Update(){
	}

By |Unity3D, Video Games Development|Commenti disabilitati su Unity3D – AddComponent – Javascript

Unity3D – Position of a GameObject – JavaScript

Unity3D – Position of a GameObject – JavaScript


        // Move the object to (0, 0, 0)
	transform.position = Vector3(0, 0, 0);
		
	// Print the x component of the position to the Console
	print(transform.position.x);

By |Unity3D, Video Games Development|Commenti disabilitati su Unity3D – Position of a GameObject – JavaScript