Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice
  3. Join us on November 16th, 2023, between 1 pm and 9 pm CET for Ask the Experts Online on Discord and on Unity Discussions.
    Dismiss Notice

[Solved] Code not working after update

Discussion in 'Scripting' started by Richard_Roth, Mar 4, 2016.

  1. Richard_Roth

    Richard_Roth

    Joined:
    Dec 29, 2014
    Posts:
    61
    This code was working fine before the update (5.2 to 5.3) and now it's not working as intended and I can't figure out why.

    This code instantiates a bar at a certain position (Flow Bar Bit Start) before the update, but now it just instantiates at the middle of the screen (objectPos).

    I can't figure out why, has the update messed with anything?

    Code (csharp):
    1.  
    2.  
    3.     //flowBar
    4.     public static int flow;
    5.     private int flowBits = 80;
    6.     public int flowBitNumber;
    7.     public int flowBitOpacity;
    8.     //private bool isDone = false;
    9.     private GameObject[] flowBitsArray = new GameObject[80];
    10.     private GameObject tempFlowBit;
    11.     [SerializeField]
    12.     private RectTransform tempFlowStart;
    13.     [SerializeField]
    14.     private GameObject flowBarBitStart;
    15.  
    16.  void FlowBarInstantiate()
    17.     {
    18.             //flowBits is set to 80 (80 individual bits) which make up the flow bar
    19.             for (int i = 0; i < flowBits; i++)
    20.             {
    21.                 //instantiates the gameobjects from a prefab
    22.                 //tempFlowBit = Instantiate(flowBarBit, objectPos, Quaternion.identity) as GameObject;
    23.                 tempFlowBit = Instantiate(flowBarBit, objectPos, Quaternion.identity) as GameObject;
    24.             Debug.Log("object pos:" + objectPos);
    25.                 if (tempFlowBit == null) return;
    26.                 //sets bits to the ui canvas
    27.                 tempFlowBit.transform.SetParent(canvas.transform, false);
    28.                 //sets bit position and placement
    29.                 tempFlowBit.GetComponent<RectTransform>().position = new Vector4(tempFlowStart.position.x + (0.05f * i), tempFlowStart.position.y, tempFlowStart.position.z);
    30.             //this sets the colour. For each iteration of the for loop it reduces the red colour and increases the green colour.
    31.             Debug.Log(tempFlowBit.GetComponent<RectTransform>().position);
    32.  
    33.                 //places bits in an array
    34.                 flowBitsArray[i] = tempFlowBit;
    35.                 tempFlowBit.SetActive(false);
    36.                            
    37.             }
    38.  
    39.         flowBarBitStart.SetActive(false);
    40.     }
    41.  
     
  2. Marikosama

    Marikosama

    Joined:
    Jun 26, 2015
    Posts:
    13
    Try to use Canvas.ForceUpdateCanvases before you set any position
     
  3. Richard_Roth

    Richard_Roth

    Joined:
    Dec 29, 2014
    Posts:
    61
    Uh... well that was dumb.

    3 hours playing with code only to realise that I was calling it in Awake() instead of Start()...

    Thanks for the help in any case