Home c# arrays in unity

arrays in unity

Author

Date

Category

Trying from the array to transfer objects to Instantiate, everything seems to be written correctly errors when you start not, but also tell me why?

using unityengine;
Using System.Collections;
Public Class Mapgenirator: Monobehaviour
{
  Public Enum Listblock
  {
    Mpole1
    Mpole2.
  }
  Public Transform SB_MPOLE1;
  Public Transform SB_MPOLE2;
  Public Transform Ground;
  TRANSFORM RETURNLIST (Listblock RL)
  {
    Switch (RL)
    {
      Case Listblock.mpole1:
        RETURN SB_MPOLE1;
      Case Listblock.mpole2:
        RETURN SB_MPOLE2;
      Default:
        RETURN SB_MPOLE1;
    }
  }
  ListBlock [,] ArrayListBlock;
  Void CreateLevel ()
  {
    for (int i = 0; i & lt; 10; ++ i)
    {
      for (int j = 0; j & lt; 10; ++ j)
      {
        float positionblock_x = transform.position.x + sb_mpole1.localscale.x;
        float positionblock_z = transform.position.z + sb_mpole1.localscale.z;
        float positionblock_y = transform.position.y + 10f;
        Transform Addarrey = ReturnList (ArrayListBlock [i, J]);
        Transform Createblock = (Transform) Instantiate (sb_mpole1, new vector3 (positionblock_x, positionblock_y, positionblock_z), quaternion.identity);
      }
    }
  }
  Void Fullarrey ()
  {
    for (int i = 0; i & lt; 10; ++ i)
    {
      for (int j = 0; j & lt; 10; ++ j)
      {
         Arraylistblock [i, j] = listblock.mpole1;
      }
    }
  }
  Void Sharing ()
  {
    Fullarrey ();
    CreateLevel ();
  }
  Void Start ()
  {
    CreateLevel ();
    Arraylistblock = new listblock [10,10];
  }
}

Answer 1, Authority 100%

I would do everything a bit wrong.

would create an array of objects

public transform [] prefabs;

And then I would instance it

Instantiate (prefabs [Random.Range), New Vector3 (I * 2.0F, 0, 0), quaternion.identity);

Now it will be randomically instating an object from array

Notice Work with Instantiate in units. The problem of what I did not understand, but the essence seems to be caught, my version is like this.


Answer 2, Authority 100%

Do you have start () with a small letter “S”. It’s a typo? If so conceived, then your code is simply not called.

To be called by the engine, it is necessary to impreditate the monobehaviour.start () :

void start ()
  {
    CreateLevel ();
    Arraylistblock = new listblock [10,10];
  }

Programmers, Start Your Engines!

Why spend time searching for the correct question and then entering your answer when you can find it in a second? That's what CompuTicket is all about! Here you'll find thousands of questions and answers from hundreds of computer languages.

Recent questions