function Start () {
// Make a game object
var lightGameObject = new GameObject ("The Light");
// Add the light component
lightGameObject.AddComponent (Light);
// Set color and position
lightGameObject.light.color = Color.blue;
// Set the position (or any transform property) after
// adding the light component.
lightGameObject.transform.position = Vector3 (0, 5, 0);
}
I translate this to C# as,
GameObject lightGameObject = new GameObject ("The Light");
lightGameObject.AddComponent(Light);
lightGameObject.light.color = Color.blue;
lightGameObject.transform.position = Vector3(0, 5, 0);
Yet it doesn’t compile. The error is,
error CS0119: Expression denotes a type', where a variable’, value' or method group’ was expected
GameObject lightGameObject = new GameObject ("The Light");
//Change occurs on the line below
lightGameObject.AddComponent<Light>();
lightGameObject.light.color = Color.blue;
lightGameObject.transform.position = Vector3(0, 5, 0);
I think that means that Resources.Load(“enemy”) is not a valid resources.
Does that particular object load in UnityScript?
When Resources.Load can’t find the specified file it will return null. This causes Instantiate to crash out because you just told it to load the null returned from Resources.Load.
The other possible option is that the Enemy prefab being returned is being returned in an unusual format. However Instantiate requires a lower level of object so it should be able to convert System.Object from UnityEngine.Object.
And I found that the terms used by Unity3D is very strange:
Returns the asset at path if it can be found otherwise returns null. The path is relative to the Resources folder, extensions must be omitted. The Resources folder can be anywhere inside the Assets folder.
So the question here is,
do I need to make a folder name “Resources” under “Assets”?
“Path is relative to the Resources folder”, what does this mean? Do I need to specify a path. If so, how come none of the examples specify a path at all. If no path is needed, why the heck the “path” stuff is mentioned here at all?!
If no path should be specified, the statement should better be re-phrased something like,
“You should put all your resources files in sub-folders under the Assets folder”.
If on the other hand, if path is needed to be specified, this should be shown in the examples.
Finally, I figure this part out. The manual should explain this clearly,
Unity3D will not accept other folder names, except for “Resources” if you want to load resources programmatically.
So as a result, you need to,
make a folder named “Resources” (other names will never work for you (hey, this should be mentioned in the manual).
you need to use all lowcase even though your resource’s file name is in captitals. Say, if the name of your testure resource is “Grass”, you have to code it as (Resources.Load(“grass”).
I think I spent about 5 hours pulling my hair out wondering why resources were not loading. It is listed in the Script Reference, but I glossed over it the first time and marked that paragraph as read.
A bit awkward to get going, but once you get the rules it should work.