• Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by 2dkot · Oct 02, 2011 at 09:39 AM · instantiateprefab

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

Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

1 Reply

· Add your reply
  • Sort: 
avatar image
1
Best Answer

Answer by Bunny83 · Oct 02, 2011 at 11:24 AM

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(...)


Comment
Add comment · Show 4 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image 2dkot · Oct 02, 2011 at 12:03 PM 0
Share

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

cloneStone01.name = cloneStone01.name + "01"

avatar image 2dkot · Oct 02, 2011 at 07:44 PM 0
Share

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)

avatar image Bunny83 · Oct 03, 2011 at 02:09 AM 0
Share

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.

avatar image 2dkot · Oct 03, 2011 at 07:27 PM 0
Share

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.

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Welcome to Unity Answers

If you’re new to Unity Answers, please check our User Guide to help you navigate through our website and refer to our FAQ for more information.

Before posting, make sure to check out our Knowledge Base for commonly asked Unity questions.

Check our Moderator Guidelines if you’re a new moderator and want to work together in an effort to improve Unity Answers and support our users.

Follow this Question

Answers Answers and Comments

3 People are following this question.

avatar image avatar image avatar image

Related Questions

Associate objects to a prefab 1 Answer

instantiating vertically 2 Answers

How to Add Y Axis Offset to transform.localPosition on an Instantiated Prefab? 2 Answers

Instantiating one prefab with different materials in one frame 0 Answers

how to destroy camera instatiated from prefab? 0 Answers


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges