#pragma strict & Instantiate Warning message

Hi Guys,

I noticed the newer version of unity, the script editor, always put “#pragma strict” on top

  1. What is #pragma strict, #pragma downcast & #pragma implicit?
    From what i read, it will always use #pragma strict in mobile no matter what, so it’s good to leave it there to remind myself that I’m always typing in #pragma strict mode.

How does each affect other when should I put #pragma downcast & #pragma implicit inside my code too?

  1. I get an warning for instantiate, how do I solve it? I read some of the answers about it.
    I tried var someObject : GameObject = Instantiate(somePrefab, Vector3.zero, Quaternion.identity); But it’s still has the same warning.

Then I tried adding #pragma downcast at the top, and its work. or Removing the #pragma strict, it will remove the warning message.
What is going on actually? I don’t really understand even though i managed to get rid the warning.

I know that putting #pragma strict is good for doing coding, but by putting #pragma downcast is like suppressing it, isn’t bad?

Below is the code:

 #pragma strict
var numberOfTiles = 16;
var tileObject : GameObject;
var tileLocations = new Array
	Vector3(0,0,0), Vector3(1.5,0,0), Vector3(3,0,0), Vector3(4.5,0,0),
	Vector3(0,1.5,0), Vector3(1.5,1.5,0), Vector3(3,1.5,0), Vector3(4.5,1.5,0),
	Vector3(0,3,0), Vector3(1.5,3,0), Vector3(3,3,0), Vector3(4.5,3,0),
	Vector3(0,4.5,0), Vector3(1.5,4.5,0), Vector3(3,4.5,0), Vector3(4.5,4.5,0)

function Start () {
	Camera.main.transform.position = Vector3(2.25,2.25,-8);
	for (var i=0; i < numberOfTiles; i++)
		Instantiate (tileObject, tileLocations*, Quaternion.identity);*
  • }*

Warning of the Instantiate line: BCW0028:WARNING: Implicit downcast from ‘Object’ to ‘UnityEngine.Vector3’.
Thanks in advance!

#pragma strict forces you to define all variables and explicitly downcast a return value to the type of the variable if that variable is a subclass.

#pragma implicit enables you to use variables without declaring them (not really a good idea).

#pragma downcast enables you to cast to a sub type - that is what is happening in your Instantiate line - you have an array of objects but you want to use a Vector3. You can do an explicit cast like this:

   var someGameObject : GameObject = Instantiate(somePrefab) as GameObject;

But you can’t do it with a struct.