- Home /
Creating a pointer variable to a GameObject inside a class that does not extend MonoBehavior? [C#]
I have a data structure class that does not extend MonoBehavior. Inside of that class I want to store a pointer to a GameObject (actually several). I have searched but came up empty. I am sure it is easy I just don't know exactly what I am looking for. How do I do it?
Note: as I am researching, it appears that a GameObject variable is itself a pointer? I have not confirmed this in any real documentation. Can anyone confirm this?
Important because... if this is the case, then can't I just use/assign a normal GameObject variable like I usually would? (as long as my script imports the UnityEngine)
Answer by Bunny83 · Oct 20, 2014 at 01:05 AM
I think you have to start to use the right terms as in C# there are actual pointers (only available in an unsafe context) and references. In C# most the time you will work with references and not pointers. Depending on your target platform unsafe code isn't supported at all. Even if it's possible you have to add the "-unsafe" compiler switch so unity even compiles such code.
You have to be more precise when you say you want to "store" a reference to one or more GameObjects. Do you mean store at runtime or do you mean store the references as edit time and have those references serialized so you can use them at runtime?
The first case shouldn't make any problems. If you have a custom class (not inherited from MonoBehaviour) you can of course have GameObject fields (variables) in your class and you can assign a GameObject reference to this variable.
The second case is more complicated since Unity does only serialize classes derived from UnityEngine.Object. Things are always stored as "assets" at edit time. There are different kind of assets types. A GameObject with a MonoBehaviour derived class might be stored as prefab or along with a scene asset. To be able to store a standalone asset that isn't a GameObject you have to derive your class from ScriptableObject. However you have to write an editor script to actually create and store such an instance.
Unity also provides a ways to use custom classes like structs by adding the System.Serializable attribute to your custom class. This class can't be serialized on it's own, but it can be serialized along with a MonoBehaviour or a ScriptableObject.
Example:
[System.Serializable]
public class MyCustomClass
{
public string someStringData;
public GameObject aGameObjectReference;
}
public class MyComponent : MonoBehaviour
{
public MyCustomClass obj1;
public MyCustomClass obj2;
}
"MyCustomClass" is treated like a struct when Unity serializes the MonoBehaviour. So it's values are simply stored along with the MonoBehaviour.
If you want a more detailed answer, you should describe your problem more in detail, maybe with a code snippet.
Thank you, this is the info I needed. I marked it as answer. I obviously didn't understand C# Object References and was making it harder than it needed to be.
As an FYI, I just need to store a reference to a GameObject at runtime and thought I needed to do more than simply assign a GameObject.
Answer by eu-ge-ne · Oct 19, 2014 at 11:18 PM
You can easily find any GameObject
by name or tag:
private GameObject Cam {
get {
return GameObject.Find("Main Camera");
// or
// return GameObject.FindGameObjectWithTag("TagName")
}
}
If You are actually want a reference to a component, attached to the GameObject:
private Camera Cam {
get {
return GameObject.Find("Main Camera").GetComponent<Camera>();
}
}
Thanks for the reply. I already have the object, I was just wondering how to store a pointer to it.But, I'm starting to think I was missing some important knowledge...
Your answer suggests that a regular GameObject variable is already a pointer, is that the case? Can I just go something like...
GameObject myPointerToGameObject = someGameObject;
And both someGameObject and myPointerToGameObect will both be pointers to the same object? Super easy if that's the case.
Your answer
Follow this Question
Related Questions
How to pass a class without it being a pointer/reference? 1 Answer
Alternative to using prefabs w/scripts to save sets of data for use at runtime? 1 Answer
Storing References of Custom Classes 0 Answers
How may i get a variable from another script onto my wanted Script? 1 Answer
Is it possible for a non-MonoBehaviour class to use built-in functions? 1 Answer