- Home /
Question by
Trevdevs · Aug 18, 2016 at 04:00 AM ·
c#uirecttransform
Changing Rect Position in code causes object to disappear? (C#)
So what i'm trying to do is create a searchable inventory which i have done, but then place the item or in this case image with a text element at the top of a scroll view since there is no way for it to move automatically when your setting the others setactive to false. Therefore I tried changing the rect transform position on the y to 0 since it was anchored to the top center where the sword ui element would be.
Here is the script with the correct line outlined.
using UnityEngine;
using System.Collections;
using UnityEngine.UI;
public class SearchBag : MonoBehaviour {
public InputField searchBar;
public ScrollRect bag;
void Start()
{
searchBar.onEndEdit.AddListener(delegate {ReturnText(searchBar);});
}
void ReturnText(InputField input)
{
for (int i = 0; i < bag.content.childCount; i++)
{
if (searchBar.text == bag.content.GetChild (i).gameObject.name)
{
bag.content.GetChild (i).gameObject.SetActive (true);
//This should be setting the rect position to its current x, 0, 0.
//Dont ask why i did vector3 on a ui element ;)
//===========================================================================
bag.content.GetChild (i).gameObject.GetComponent<RectTransform> ().position =
new Vector3 (bag.content.GetChild (i).gameObject.GetComponent<RectTransform> ().position.x, 0, 0);
//============================================================================
}
else if (searchBar.text == "")
{
bag.content.GetChild (i).gameObject.SetActive (true);
}
else
{
bag.content.GetChild (i).gameObject.SetActive (false);
}
}
}
}
However as seen in this photo it sets the y value to -600 for whatever reason 0 should be correct as that is the y value of the sword ui element.
screenshot-18.png
(178.4 kB)
screenshot-19.png
(169.8 kB)
Comment
knowing me its probably something simple and i'm to stupid to see it :)