UI Scroll View object is not moving
Hi i generated an object via editor. It is child object of "Scroll view/view port/content".I want to change its position, but i can't. I deleted all codes from object, if i try via "Start" function it's working. but if i call from another function it's not. Object has rigidbody2d and boxcollider2d component and its a UI object it has selectable, image and text component.I tried all combination of localPosition, translate, setParent, transform.parent, transform.position, rectTransform, rectAnchors.
Below codes how i generate object
for (int j = 0; j < anschars.Count; j++)
{
if (j == k1)
{
fieldtemp.GetComponent<testfield>().tempquestionno = questionno;
fieldtemp.GetComponent<testfield>().tempcharno = j;
fieldtemp.GetComponent<testfield>().doublequestion = true;
if (questionno % 2 == 0)
{
fieldtemp.GetComponent<testfield>().updownquestionno = updownqn;
fieldtemp.GetComponent<testfield>().updowncharno = j + 1;
}
else if(questionno %2==1)
{
fieldtemp.GetComponent<testfield>().leftrightquestionno = leftrightqn;
fieldtemp.GetComponent<testfield>().leftrightcharno = j + 1;
}
}
else
{
if (questionno % 2 == 0)
{
fieldins = Instantiate(field1, new Vector3(fieldtemp.transform.position.x, fieldtemp.transform.position.y - ((j - k1) * 30f)), field1.transform.rotation, GameObject.FindGameObjectWithTag("Content").transform);
fieldins.GetComponent<testfield>().truecharvalue = anschars[j].ToString();
fieldins.GetComponent<testfield>().tempquestionno = questionno;
fieldins.GetComponent<testfield>().tempcharno = j;
fieldins.GetComponent<testfield>().updownquestionno = updownqn;
fieldins.GetComponent<testfield>().updowncharno = j + 1;
fieldins.GetComponent<testfield>().updownposition = new Vector2(fieldins.GetComponent<testfield>().leftrightposition.x,contenttransform.position.y + (anschars.Count * 20f) - (j * 40f));
fieldpositions.Add(fieldins.transform.position);
}
if (questionno % 2 == 1)
{
fieldins = Instantiate(field1, new Vector3(fieldtemp.transform.position.x + ((j - k1) * 30f), fieldtemp.transform.position.y), field1.transform.rotation, GameObject.FindGameObjectWithTag("Content").transform);
fieldins.GetComponent<testfield>().truecharvalue = anschars[j].ToString();
fieldins.GetComponent<testfield>().tempquestionno = questionno;
fieldins.GetComponent<testfield>().tempcharno = j;
fieldins.GetComponent<testfield>().leftrightquestionno = leftrightqn;
fieldins.GetComponent<testfield>().leftrightcharno = j + 1;
fieldins.GetComponent<testfield>().leftrightposition = new Vector2(contenttransform.position.x - (anschars.Count * 20f) + (j * 40f), fieldins.GetComponent<testfield>().leftrightposition.y);
fieldpositions.Add(fieldins.transform.position);
}
}
and its function attached to object, i call it via button. I can see debug.log
public void LeftRightReposition()
{
Debug.Log("leftrightposition");
//this.transform.parent = content.transform;
//this.transform.SetParent(content.transform);
transform.position = content.transform.position + leftrightposition;
}
Could you try debugging to see if position is actually changing? By debugging, (setting a breakpoint and stepping through the code) I'm wondering what leftrightposition's value is when you press that button. $$anonymous$$y guess is that it's at (0, 0, 0), so nothing happens.