Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  2. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

Instantiating C#??

Discussion in 'Scripting' started by Wikened, Jan 28, 2012.

  1. Wikened

    Wikened

    Joined:
    Oct 31, 2011
    Posts:
    271
    Code (csharp):
    1. for(int x = 0; x < Cubes.Length; x++)
    2.         {
    3.             for(int y = 0; y < Cubes.Length; y++)
    4.             {
    5.                 for(int z = 0; z < Cubes.Length; z++)
    6.                 {
    7.                     Instantiate(Cube,new Vector3((float)x,(float)y,(float)z),Quaternion.identity);
    8.                 }  
    9.             }      
    10.         }
    My error:
    Assets/GenerateWorld.cs(31,41): error CS1502: The best overloaded method match for `UnityEngine.Object.Instantiate(UnityEngine.Object, UnityEngine.Vector3, UnityEngine.Quaternion)' has some invalid arguments
     
  2. nikolic

    nikolic

    Joined:
    Oct 15, 2011
    Posts:
    162
    There should be the issue with the "Cube", as Vector3 and Quaternion seem fine.
    Can you post the code where variable "Cube" is declared? I guess it's supposed to be a prefab. If so, have you set the prefab value in Inspector?
     
  3. 3dborat

    3dborat

    Joined:
    Jun 17, 2010
    Posts:
    27
    This line:
    Code (csharp):
    1.  
    2. Instantiate(Cube,new Vector3((float)x,(float)y,(float)z),Quaternion.identity);
    3.  
    should be:
    Code (csharp):
    1.  
    2. Instantiate(Cube,new Vector3((float)x,(float)y,(float)z),Quaternion.identity) as GameObject;
    3.  
     
  4. foxter888

    foxter888

    Joined:
    May 3, 2010
    Posts:
    530
    for the instantiate command in c# you need to cast it to a gameObjec like this

    GameObject clone;
    clone = Instantiate(Cube,new Vector3((float)x,(float)y,(float)z), Quaternion.identity) as GameObject;