Search Unity

How to create a model from script?

Discussion in 'Scripting' started by Hyder, Oct 3, 2013.

  1. Hyder

    Hyder

    Joined:
    Oct 3, 2013
    Posts:
    1
    Hi! Im looking some functions or example (C#). How to add new 3ds model from script to game scene?
     
  2. wccrawford

    wccrawford

    Joined:
    Sep 30, 2011
    Posts:
    2,039
  3. Marsupilami

    Marsupilami

    Joined:
    Sep 22, 2013
    Posts:
    21
    Are you talking about Instantiate an object during runtime from a prefab?

    You could also put this tied to a button or timer or anywhere needed... In my example below I have an empty game object with the following script tied to it with a standard cube saved as a prefab associated with the public variable cubePrefab. This creates 1000 cubes in the center of the scene in the shape of a larger cube.

    Code (csharp):
    1.  
    2. public class GameStartup : MonoBehaviour {
    3.    
    4.     public GameObject cubePrefab;
    5.    
    6.     void Awake ()
    7.     {
    8.         for (int i1 = 0; i1 < 10; i1++)
    9.         {
    10.             for (int i2 = 0; i2 < 10; i2++)
    11.             {
    12.                 for (int i3 = 0; i3 < 10; i3++)
    13.                 {
    14.                     float xp = (i1 - 9.5f * 2);
    15.                     float yp = (i2 - 9.5f * 2);
    16.                     float zp = (i3 - 9.5f * 2);
    17.                     Instantiate (cubePrefab, new Vector3(xp, yp, zp), Quaternion.identity);
    18.                 }
    19.             }
    20.         }
    21.     }
    22. }
    23.  

    If you are talking about loading a model from a raw file during runtime using the os filesystem, I'm not sure why anyone would want to do that unless they had a very large game and wanted to save on resources until they needed them. In that case, I can't help with that except advise to look in the documentation for answers.