Search Unity

Assign Instantiate return value to a GameObject in for

Discussion in 'Scripting' started by MikeyJY, Apr 26, 2020.

  1. MikeyJY

    MikeyJY

    Joined:
    Mar 2, 2018
    Posts:
    530
    Ok I have this foreach in foreach loop:
    Code (CSharp):
    1. for(int i = 0; i < ItemOrder.Length; i++)
    2.             {
    3.                 for(int items = 0; items < ItemsLoad[i]; items++)
    4.                 {
    5.                     Instantiate(ItemOrder[i], ItemOrder[i].GetComponent<Inventory>().position[items], Quaternion.Euler(ItemOrder[i].GetComponent<Inventory>().rotation[items]));
    6.                  
    7.                 }
    8.             }
    It is working but I want to adjust the scale and some rigidbody values. I tried this:
    Code (CSharp):
    1. for(int i = 0; i < ItemOrder.Length; i++)
    2.             {
    3.                 for(int items = 0; items < ItemsLoad[i]; items++)
    4.                 {
    5.                     GameObject Item = Instantiate(ItemOrder[i], ItemOrder[i].GetComponent<Inventory>().position[items], Quaternion.Euler(ItemOrder[i].GetComponent<Inventory>().rotation[items]));
    6.                     if(Item.GetComponent<Inventory>().useScale == true)
    7.                     {
    8.                         Item.transform.localScale = ItemOrder[i].GetComponent<Inventory>().scale;
    9.                     }
    10.                     Item.GetComponent<Rigidbody>().isKinematic = true;
    11.                     Item.GetComponent<Rigidbody>().useGravity = false;
    12.                 }
    13.             }
    But for some reason, It instantiate an object only once, after first execution the for loop breaks and I don't know why
     
  2. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    38,742
    Check the console for errors.

    Also, break up any line that contains more than one dot and assign each step to a temporary variable so that you can see what the actual problem is.
     
  3. MikeyJY

    MikeyJY

    Joined:
    Mar 2, 2018
    Posts:
    530
    No errors in console