Thank you for helping us improve the quality of Unity Documentation. Although we cannot accept all submissions, we do read each suggested change from our users and will make updates where applicable.
CloseFor some reason your suggested change could not be submitted. Please <a>try again</a> in a few minutes. And thank you for taking the time to help us improve the quality of Unity Documentation.
ClosehostObj | The host object that supports SerializeReference. For example, MonoBehaviour or ScriptableObject. |
refId | The managed reference Id. |
object Returns the C# object referenced on the specified host and identified with provided Id. Returns null if no reference object is found.
Retrieves an object based on its managed reference Id.
Use this method to efficiently retrieve a specific referenced object when, for example, its exact location within an array or graph is unknown.
Additional resources: GetManagedReferenceIdForObject, SerializeReference.
using System; using System.Collections.Generic; using UnityEditor; using UnityEngine; using UnityEngine.Serialization;
public class GetManagedReferenceExample : ScriptableObject { [Serializable] public class Item { public char m_data; }
[SerializeReference] public List<Item> m_items = new List<Item>();
private void InsertNewItem(long id, char data) { var newItem = new Item() {m_data = data}; if (ManagedReferenceUtility.SetManagedReferenceIdForObject(this, newItem, id)) m_items.Add(newItem); }
[MenuItem("Example/ManagedReferenceUtility GetManagedReference Example")] static void TestMethod() { var scriptableObject = ScriptableObject.CreateInstance<GetManagedReferenceExample>();
scriptableObject.InsertNewItem(1000, 'a'); scriptableObject.InsertNewItem(1001, 'b'); scriptableObject.InsertNewItem(1002, 'c'); scriptableObject.InsertNewItem(1003, 'd');
// Because 1002 is registered in an earlier call this will log an error and not change the state scriptableObject.InsertNewItem(1002, 'e');
// The array may get reordered over time. One way to find an specific item again would // be to look it up based on a known managed reference id var item = ManagedReferenceUtility.GetManagedReference(scriptableObject, 1002) as Item; Debug.LogFormat("Data on object 1002 is {0}", item.m_data);
var willBeNull = ManagedReferenceUtility.GetManagedReference(scriptableObject, 9999) as Item; } }
Did you find this page useful? Please give it a rating:
Thanks for rating this page!
What kind of problem would you like to report?
Thanks for letting us know! This page has been marked for review based on your feedback.
If you have time, you can provide more information to help us fix the problem faster.
Provide more information
You've told us this page needs code samples. If you'd like to help us further, you could provide a code sample, or tell us about what kind of code sample you'd like to see:
You've told us there are code samples on this page which don't work. If you know how to fix it, or have something better we could use instead, please let us know:
You've told us there is information missing from this page. Please tell us more about what's missing:
You've told us there is incorrect information on this page. If you know what we should change to make it correct, please tell us:
You've told us this page has unclear or confusing information. Please tell us more about what you found unclear or confusing, or let us know how we could make it clearer:
You've told us there is a spelling or grammar error on this page. Please tell us what's wrong:
You've told us this page has a problem. Please tell us more about what's wrong:
Thank you for helping to make the Unity documentation better!
Your feedback has been submitted as a ticket for our documentation team to review.
We are not able to reply to every ticket submitted.