Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 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 Nemox · Jun 27, 2011 at 05:33 PM · variablestaticinstancenew

Having trouble with static references.

So I have a Local Players script which keeps track of four Player singletons. Each player singleton refers to a Camera instance, which is supposed to be limited to 1 per player. But every time I refer to the camera instance, it keeps spawning new ones, although the reference remains on the real one.

Here are the important bits. What could I be doing wrong?

 public class Players : MonoBehaviour {
 
 private static Player _player1;
 
 public static Player p1 {
     get {
         if(_player1 == null) {
             _player1 = new Player(1);
         }
         return _player1;
     }
 }
 }

 public class Player {
     
     //Private Variables
     private bool _active;
     private Camera _camera;
 
 
     //Class Variables
     public bool active {
         get {
             if(_active == true) { camera.enabled = true; }
             else { camera.enabled = false; }
             return _active;
         }
         set {
             if(value == true) { camera.enabled = true; }
             else { camera.enabled = false; }
             _active = value;
         }
     }
     
     public Camera camera {
         get {
             if(_camera == null) {
                 _camera = (new GameObject().AddComponent("Camera")).camera;
                 _camera.gameObject.name = "Player Camera";
                 Object.DontDestroyOnLoad(_camera);
             }
             return _camera;
         }
         set {
             if(_camera != null) {
                 Object.Destroy(_camera);
             }
             _camera = value;
         }
     }
 }

 public class Motor : MonoBehaviour {
         Player _player;
     void Awake() {
         _character = GetComponent(typeof(Character)) as Character;
         _player = _character.player;
         if(_player != null) {
             _cam = _player.camera;  //RIGHT HERE.  This little guy keeps making my Player script spawn new cameras.
         }
         
         rigidbody.freezeRotation = true;
         characterCollider = (CapsuleCollider)GetComponent(typeof(CapsuleCollider));
     }
 }
Comment
Add comment
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

1 Reply

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

Answer by TowerOfBricks · Jun 27, 2011 at 07:18 PM

I'm not sure if this is the correct answer since you should be getting a NullReferenceException, but anyway. When you are creating the _camera gameObject, you are adding a PlayerCamera component (whatever that is), but you are not adding a Camera component (which should cause the call to .camera to return null). But perhaps your PlayerCamera script has a RequireComponent attribute and that adds it. Otherwise I cannot see anything wrong with the code.

And also, your SerializeField attributes won't do anything since your class (Player) isn't serializable, mark it with [Serializable].

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 Nemox · Jun 27, 2011 at 07:24 PM 0
Share

Ah yeah, that script requires a camera. $$anonymous$$y SerializeField stuff was for debugging and such. I'll edit it out.

Very odd. For some reason, it just keeps adding a camera every time I make a new character with that Character$$anonymous$$otor script. That line "_cam = _player.camera;" seems to be the culprit. It references a player, which seems to work fine on its own, but as soon as I reference the camera through its getter, it creates a new one, yet still references the original.

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

3 People are following this question.

avatar image avatar image avatar image

Related Questions

An instance of type X is required to access non static member Y [javascript] 4 Answers

Problem setting up an array with a size determined by a variable 2 Answers

Prefab's Script affecting all the Prefab 1 Answer

Static, yet Private, Variables? 1 Answer

Passing Object References Through Function 0 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