Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by DevLurkin · Oct 19, 2014 at 10:36 PM · classreferencemonobehaviourpointer

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?

Comment
Add comment · Show 1
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image DevLurkin · Oct 20, 2014 at 12:32 AM 0
Share

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)

2 Replies

· Add your reply
  • Sort: 
avatar image
1
Best Answer

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.

Comment
Add comment · Show 1 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image DevLurkin · Oct 20, 2014 at 01:13 AM 0
Share

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.

avatar image
0

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>();
     }
 }
Comment
Add comment · Show 1 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image DevLurkin · Oct 20, 2014 at 12:53 AM 0
Share

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

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

29 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

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


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges