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

Assign GameObject to ObjectField from objectReferenceValue

Discussion in 'UI Toolkit' started by dan-kostin, Feb 3, 2020.

  1. dan-kostin

    dan-kostin

    Joined:
    Oct 19, 2018
    Posts:
    22
    Code (CSharp):
    1. var property = m_TypesProperty.GetArrayElementAtIndex(index);
    2. var obj = property.objectReferenceValue as GameObject;
    3. scrollView.Add(new ObjectField() { value = obj});
    I've debuged this and property.objectReferenceValue is not null but on new ObjectField() { value = obj} I get
    Any ideas what is wrong and how to sole this?
     
  2. pirho_luke

    pirho_luke

    Joined:
    Oct 24, 2017
    Posts:
    21
    This is because ObjectField needs to have an objectType set. This should work:

    Code (CSharp):
    1. scrollView.Add(new ObjectField
    2. {
    3.    objectType = typeof(GameObject),
    4.    value = obj
    5. });
     
    Robert_Luminous and dan-kostin like this.