Unknown identifier: 'Instantiate' - weird behavior.

Hi,

I try to instantinate my game object and place it over terrain. More over I group game objets to defferents sets - for this script call special function.

Function to initializ game object:

class gStone{

var stone_01: GameObject;
var grass_02: GameObject;
var grass_03: GameObject;
var grass_04: GameObject;
var gMaterial: Material;
var stone: GameObject;

function getBigStone01():GameObject{

	stone = Resources.Load("Stones/bigStone01",GameObject);
	stone.transform.localScale = Vector3(4,4,4);
	return stone;
}
function getBigStone02():GameObject{

	stone = Resources.Load("Stones/bigStone02",GameObject);
	stone.transform.localScale = Vector3(5,5,5);
	return stone;
}
function getStone01(size:Vector3):GameObject{

	stone = Resources.Load("Stones/stone01",GameObject);
	stone.transform.localScale = size;
	return stone;
}
function getStone02(size:Vector3):GameObject{

	stone = Resources.Load("Stones/stone02",GameObject);
	stone.transform.localScale = size;
	return stone;
}

}

Function to create groups:

    class gStoneGroups{
	
var stone01:GameObject;
var stone02:GameObject;
var stone03:GameObject;
var mainPos:Vector3;
var stone:gStone;
var botAction: botActions;

	var cloneStone01:GameObject;
	var cloneStone02:GameObject;
	var cloneStone03:GameObject;
	var cloneStone04:GameObject;
	var cloneStone05:GameObject;

function getSimpleTerrainRotY(terrPos: Vector3){
    var terrHit: RaycastHit;
	var normal_q: Quaternion;
	terrPos.y+=20;
	
    if(Physics.Raycast (terrPos, -Vector3.up, terrHit, 10.0)){
		
		normal_q = Quaternion.FromToRotation(Vector3.up, terrHit.normal);
    }

	return Quaternion.Euler(270, 0, normal_q.z);
	
}

function group00(target:Vector3){
	var pos01: Vector3 = botAction.getPosAndTerrainY(Vector3(target.x+2,target.y,target.z));
	var pos02: Vector3 = botAction.getPosAndTerrainY(Vector3(target.x+6,target.y,target.z+6));
	var pos03: Vector3 = botAction.getPosAndTerrainY(Vector3(target.x+1,target.y,target.z+6));
	var pos04: Vector3 = botAction.getPosAndTerrainY(Vector3(target.x-1,target.y,target.z-6));
	
	//mainPos = target;
	cloneStone01 = Instantiate(stone.getBigStone01(), pos01, getSimpleTerrainRotY(pos01));
	cloneStone02 = Instantiate(stone.getBigStone02(), pos02, getSimpleTerrainRotY(pos02));
	cloneStone03 = Instantiate(stone.getStone01(Vector3(0.5,0.5,0.5)), pos03, getSimpleTerrainRotY(pos03));
	cloneStone04 = Instantiate(stone.getStone02(Vector3(1,1,1)), pos04, getSimpleTerrainRotY(pos04));
	
}
}

Function to call Group of game object:

var stoneGroup:gStoneGroups;

stoneGroup.group00(Vector3(100,100,100));

In this case I see exception:

Assets/gScripts/classes/Environment/gStoneGroups.js(37,24): BCE0005: Unknown identifier: ‘Instantiate’.

Thanks

That is really bizarre. Ive never had that issue before. If the animation component is empty, why would it do anything to the object?

No idea, also @ravingLunatic Please don't post comments as answers.

What do you have to do when you want animation on the object? Cause mine object disappears when adding an animation.

1 Answer

1

Instantiate is a static function of UnityEngine.Object. Normal scripts that derive from MonoBehaviour are also derived from UnityEngine.Object and therefore in the scope of UnityEngine.Object. Your custom class isn’t derived from MonoBehaviour or UnityEngine.Object and therefore Instantiate is unknown inside your class.

Just use the usual way:

Object.Instantiate(...)

Done. It works. But when I try to change name of Object, prefabs do not appears in scene. cloneStone01.name = cloneStone01.name + "01"

I have this exception when I try to change name: cloneStone01.name = cloneStone01.name + "01" Exception: NullReferenceException: Object reference not set to an instance of an object gStoneGroups.group00 (Vector3 target) (at Assets/gScripts/classes/Environment/gStoneGroups.js:43)

Are you sure that you try to set the name after Instantiate? Also make sure that you placed your prefabs in Assets/Resources/Stones/ and you use the right filenames. The error is in line 43 so you should check that one. If the error occures in the cloneStone01.name = cloneStone01.name + "01" line the object wasn't created most likely because the prefab couldn't be found.

Problem is that when I try to rename prefab do not appears in scene, everything is fine in case I leave name as it is.