Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 14 Next capture
2021 2022 2023
2 captures
12 Jun 22 - 14 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 MIHGames · Oct 24, 2016 at 06:36 AM · c#unity 5bug-perhapsmonobehaviourstatic class

GameObject is null when assigning to non-monobehaviour public static class

Alright. I've given up. I can't seem to figure this one out. I have a MonoBehaviour script that is attached to an object, and I've got a public static class that isn't monobehaviour. Here is my code: Non monobehaviour script:

 using UnityEngine;
 using System.Collections;

 public static class GameManager
 {
     //public GameObject Map;
     private static GameObject _Map;
     private const int _connectorLayer = 8;
     public static GameObject Map
     {
         get
         {
             return _Map;
         }
         set
         {
             Debug.Log(Map);
             _Map = Map;
 
         }
     }
 }

and here is my monobehaviour script that is attached to an object:

 using UnityEngine;
 using System.Collections;
 [RequireComponent(typeof(Collider))]
 public class Map : MonoBehaviour
 {
     // Use this for initialization
     new private Collider collider;
     //In world space, this is asigned in Awake.
     public float mapHeight;
 
     void Awake()
     {
         collider = gameObject.GetComponent<Collider>();
         GameManager.Map = gameObject;
         mapHeight = collider.bounds.extents.y + transform.position.y;
         //Debug.Log(collider.bounds.center.x);
     }
 }


And when I run it, GameManager outputs Null:

 Null
 UnityEngine.Debug:Log(Object)
 GameManager:set_Map(GameObject) (at Assets/Scripts/GameManager.cs:17)
 Map:Awake() (at Assets/Scripts/Map.cs:14)


I can't think of anything else this is but a bug in Unity.

Specs:

Unity 5.4.1f1 Personal

Microsoft Visual Studio Tools for Unity 2.3.0.0 enabled

Windows 10 Pro 64-bit build 14393

Included Unity Store Assets:

  • SteamVR

  • VRTK

Target Platform: Windows

Architecture: x86_64

Api Compatibility level: .NET 2.0 Subset

Scripting Backend: Mono2x

I've tried running the GameManager.Map = gameObject;in Awake(), Start() and Update(), and searching for the object and setting it in other MonoBehaviour scripts. I've also tried settings other variables, like strings using the same setup and it works fine! If I try to change GameManager.Map from a type of GameObject to a type of Map, I get the same result, null.

Comment
Add comment · Show 2
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 doublemax · Oct 24, 2016 at 07:28 AM 1
Share

Shouldn't the setter use "value"?

avatar image meb111 · Oct 24, 2016 at 07:35 AM 0
Share

I probably don't fully understand static classes as I've really only used them for singleton style classes. Have you tried making a Singleton style instance and using Game$$anonymous$$anager.Instance.$$anonymous$$ap = gameObject;

1 Reply

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

Answer by Naphier · Oct 24, 2016 at 08:40 AM

As doublemax says, the setter needs to use "value".

  public static GameObject Map
  {
      get
      {
          return _Map;
      }
      set
      {
          Debug.Log(Map);
          _Map = value;
  
      }
  }

_Map = Map... what does Map equal? Nothing. And you'd never be able to set it!

Comment
Add comment · Show 2 · 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 MIHGames · Oct 28, 2016 at 04:22 AM 1
Share

Thanks! I was desperately hoping it would be something silly like that!

avatar image Naphier MIHGames · Oct 28, 2016 at 08:48 PM 0
Share

Yup, watch those accessors, you gotta be real careful or you'll end up with issues like stack overflows :P

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

7 People are following this question.

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

Related Questions

Multiple Cars not working 1 Answer

Distribute terrain in zones 3 Answers

Unity3d Dragging object in 3d world 0 Answers

why do these both codes work similarly when they are put inside update function... 3 Answers

C# Invoke Function with Params and Delay 3 Answers


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