Search Unity

'public static class GameData' to a GameObject

Discussion in 'Scripting' started by Shonysky, Jul 6, 2015.

  1. Shonysky

    Shonysky

    Joined:
    Mar 29, 2014
    Posts:
    44
    I'm trying to add a 'public static class GameData' (my script) to a GameObject.
    But when i move the script as a component, the console says

    "Can't add script behaviour GameData. The script class can't be abstract!"

    What i have to do?
     
  2. SubZeroGaming

    SubZeroGaming

    Joined:
    Mar 4, 2013
    Posts:
    1,008
    You can't add a static class to an object.

    Remove the static. And make sure to inherit from monobehaviour
     
  3. Shonysky

    Shonysky

    Joined:
    Mar 29, 2014
    Posts:
    44
    But then, i get errors from my other scripts, like "UnityException: You are not allowed to call this function when declaring a variable."
     
  4. SubZeroGaming

    SubZeroGaming

    Joined:
    Mar 4, 2013
    Posts:
    1,008
    It would help if you pasted your scripts. Nobody can help you without seeing what you're doing wrong. Seems like these are simple rules that are being broken. Check out some tutorials (link my in signature).
     
  5. Shonysky

    Shonysky

    Joined:
    Mar 29, 2014
    Posts:
    44
    GameData script:

    Code (CSharp):
    1. using UnityEngine;
    2. using System.Collections;
    3.  
    4. public static class GameData
    5. {
    6.     public static Camera cam = Camera.main;
    7.  
    8.     public static GameObject player = GameObject.Find ("Player");
    9.  
    10.     public static Rigidbody body = player.GetComponent <Rigidbody> ();
    11.  
    12.     public static CharacterController control = player.GetComponent <CharacterController> ();
    13.  
    14.     public static bool inGame = true; // change preference
    15.     public static bool key87 = false; // w
    16.     public static bool key65 = false; // a
    17.     public static bool key83 = false; // s
    18.     public static bool key68 = false; // d
    19.     public static bool walking = false;
    20.  
    21.     public static float xRotation = 0f;
    22.     public static float yRotation = 0f;
    23.     public static float sensitivity = 10f;
    24.     public static float speed = 2f;
    25.     public static float horizontal = 0f;
    26.     public static float vertical = 0f;
    27.  
    28.     public static int camMin = -80;
    29.     public static int camMax = 80;
    30. }
    KeyBinding:

    Code (CSharp):
    1. using UnityEngine;
    2. using System.Collections;
    3.  
    4. public class KeyBinding : MonoBehaviour
    5. {
    6.     void UpdateKeyBindingSystem ()
    7.     {
    8.         if (Input.GetKeyDown (KeyCode.W))
    9.         {
    10.             GameData.key87 = true;
    11.         }
    12.         else if (Input.GetKeyUp (KeyCode.W))
    13.         {
    14.             GameData.key87 = false;
    15.         }
    16.  
    17.         if (Input.GetKeyDown (KeyCode.A))
    18.         {
    19.             GameData.key65 = true;
    20.         }
    21.         else if (Input.GetKeyUp (KeyCode.A))
    22.         {
    23.             GameData.key65 = false;
    24.         }
    25.  
    26.         if (Input.GetKeyDown (KeyCode.S))
    27.         {
    28.             GameData.key83 = true;
    29.         }
    30.         else if (Input.GetKeyUp (KeyCode.S))
    31.         {
    32.             GameData.key83 = false;
    33.         }
    34.  
    35.         if (Input.GetKeyDown (KeyCode.D))
    36.         {
    37.             GameData.key68 = true;
    38.         }
    39.         else if (Input.GetKeyUp (KeyCode.D))
    40.         {
    41.             GameData.key68 = false;
    42.         }
    43.  
    44.         if (GameData.horizontal != 0f || GameData.vertical != 0f)
    45.         {
    46.             GameData.walking = true;
    47.         }
    48.         else if (GameData.horizontal == 0f && GameData.vertical == 0f)
    49.         {
    50.             GameData.walking = false;
    51.         }
    52.     }
    53.  
    54.     void LateUpdate ()
    55.     {
    56.         UpdateKeyBindingSystem ();
    57.     }
    58. }
    Nexus:

    Code (CSharp):
    1. using UnityEngine;
    2. using System.Collections;
    3.  
    4. public class Nexus : MonoBehaviour
    5. {
    6.     void Start ()
    7.     {
    8.         GameData.xRotation = 0f;
    9.         GameData.yRotation = 0f;
    10.         GameData.sensitivity = 10f;
    11.         GameData.speed = 2f;
    12.         GameData.horizontal = 0f;
    13.         GameData.vertical = 0f;
    14.        
    15.         GameData.camMin = -80;
    16.         GameData.camMax = 80;
    17.     }
    18.  
    19.     void UpdateMovement ()
    20.     {
    21.         GameData.player.transform.rotation = Quaternion.Euler (0f, GameData.yRotation, 0f);
    22.  
    23.         GameData.horizontal = Input.GetAxisRaw ("Horizontal")*GameData.speed;
    24.         GameData.vertical = Input.GetAxisRaw ("Vertical")*GameData.speed;
    25.  
    26.         Vector3 direction = new Vector3 (GameData.horizontal, 0f, GameData.vertical);
    27.         direction = GameData.player.transform.rotation*direction;
    28.  
    29.         GameData.control.SimpleMove (direction);
    30.     }
    31.  
    32.     void UpdateCam ()
    33.     {
    34.         GameData.xRotation -= Input.GetAxis ("Mouse Y")*GameData.sensitivity*(Time.deltaTime*20f);
    35.         GameData.yRotation += Input.GetAxis ("Mouse X")*GameData.sensitivity*(Time.deltaTime*20f);
    36.  
    37.         GameData.xRotation = Mathf.Clamp (GameData.xRotation, GameData.camMin, GameData.camMax);
    38.  
    39.         GameData.cam.transform.rotation = Quaternion.Euler (GameData.xRotation, GameData.yRotation, 0f);
    40.     }
    41.  
    42.     void UpdateAllVoids ()
    43.     {
    44.         UpdateMovement ();
    45.         UpdateCam ();
    46.     }
    47.  
    48.     void LateUpdate ()
    49.     {
    50.         UpdateAllVoids ();
    51.     }
    52. }
    I'm just trying to add the GameData script (which is a class that just hold data of the player) to the Player GameObject, so that i can access player "properties" (which is the script) and assign other GameObjects "properties"
     
  6. SubZeroGaming

    SubZeroGaming

    Joined:
    Mar 4, 2013
    Posts:
    1,008
    Add this above your GameData class:

    Code (CSharp):
    1. [System.Serializable]
    ^ syntax for that may be incorrect, use the tool tip.

    In your player class, or any mono behavior, add:

    Code (CSharp):
    1. public GameData data;
    You will now be able to see the properties of GameData in the inspector on the object that has the variable data declared.
     
  7. Shonysky

    Shonysky

    Joined:
    Mar 29, 2014
    Posts:
    44
    Ok, that's helpful, but that's not what i was looking for.
    I want to just put the script as a component in my Player GameObject
     
  8. SubZeroGaming

    SubZeroGaming

    Joined:
    Mar 4, 2013
    Posts:
    1,008
    That's not doable as long as the class is static. IF you want to see the properties of that static class on your object and manipulate the data, you have todo it the way I just showed you.

    data.doSomething();

    You can't physically drag that script to the object.
     
  9. blizzy

    blizzy

    Joined:
    Apr 27, 2014
    Posts:
    775
    That's not possible with a static class. Also, components need to be derived from MonoBehaviour.

    Since your GameData class is static, why would you need to add it anyway? Just access it from code.
     
  10. LaneFox

    LaneFox

    Joined:
    Jun 29, 2011
    Posts:
    7,534
    By making something static you're saying that there is only ever ONE of that thing. A static class, then, cannot be placed as a monobehaviour because it would mean you could add many of them.

    Make a wrapper class or custom editor that modifies the static class variables, then put that on some GameObject and go to town. I'm assuming that you want to do this so you can define initial values in the inspector.
     
    HiddenMonk likes this.
  11. SubZeroGaming

    SubZeroGaming

    Joined:
    Mar 4, 2013
    Posts:
    1,008
    I haven't tested serializing static classes, but I assume what I mentioned would work? no?
     
  12. LaneFox

    LaneFox

    Joined:
    Jun 29, 2011
    Posts:
    7,534
    I haven't tested it either, but I assume it would be fine..

    Lemme whip up a test, now im curious :D
     
    SubZeroGaming likes this.
  13. SubZeroGaming

    SubZeroGaming

    Joined:
    Mar 4, 2013
    Posts:
    1,008
    Thanks. Looking forward to your results. Curious too :)
     
    LaneFox likes this.
  14. LaneFox

    LaneFox

    Joined:
    Jun 29, 2011
    Posts:
    7,534
    Serializing does not seem to work, but you can still make a custom editor to access the GameData variables easily enough.

    GameData is the static class
    Code (csharp):
    1.  
    2. public static class GameData
    3. {
    4.     public static string Name = "pants";
    5.     public static float Value = 12f;
    6. }
    7.  

    Front End which you place on some GameObject
    Code (csharp):
    1.  
    2. using UnityEngine;
    3.  
    4. public class GameDataFrontEnd : MonoBehaviour
    5. {
    6.     void Update()
    7.     {
    8.         Debug.Log(GameData.Value);
    9.         Debug.Log(GameData.Name);
    10.     }
    11. }
    Custom Editor
    Code (csharp):
    1.  
    2. using UnityEngine;
    3. using UnityEditor;
    4.  
    5. [CustomEditor(typeof(GameDataFrontEnd))]
    6. public class GameDataEditor : Editor
    7. {
    8.     private readonly GUIContent _val = new GUIContent("Value!!", "Something!");
    9.     private readonly GUIContent _name = new GUIContent("Nameeroni", "Something!");
    10.  
    11.     public override void OnInspectorGUI()
    12.     {
    13.         GUI.changed = false;
    14.  
    15.         GameData.Name = EditorGUILayout.TextField(_name, GameData.Name);
    16.         GameData.Value = EditorGUILayout.FloatField(_val, GameData.Value);
    17.  
    18.         if (GUI.changed) EditorUtility.SetDirty(target);
    19.     }
    20. }
    21.  
    However in the editor code, using ...
    Code (csharp):
    1.         EditorGUILayout.PropertyField(serializedObject.FindProperty("Value"), _val, false);
    2.         serializedObject.ApplyModifiedProperties();
    Did not work. So it does not seem like you can serialize it. Just access it via a wrapper.
     
    SubZeroGaming likes this.
  15. Shonysky

    Shonysky

    Joined:
    Mar 29, 2014
    Posts:
    44
    That's too complicated for me... I don't want to do something that i don't understand how to do it.
    But if i just put public variables inside the script (so they will be visible as a Player Component), i can reffer to them inside another script?
     
  16. LaneFox

    LaneFox

    Joined:
    Jun 29, 2011
    Posts:
    7,534
    Yes, you just have to get a reference to the script first.
     
  17. Shonysky

    Shonysky

    Joined:
    Mar 29, 2014
    Posts:
    44
    How could i do that? Scopes?
     
  18. LaneFox

    LaneFox

    Joined:
    Jun 29, 2011
    Posts:
    7,534
    If in the editor... You have to make a variable field of your script type, drag the script into it, then have code do something to that variable.

    Like
    Code (csharp):
    1. public myScript reference;
    2. ...
    3. ...
    4. reference.runSpeed = 12f;
     
  19. Shonysky

    Shonysky

    Joined:
    Mar 29, 2014
    Posts:
    44
    Also, didn't work

    Code (CSharp):
    1. using UnityEngine;
    2. using System.Collections;
    3.  
    4. public class PlayerData : MonoBehaviour
    5. {
    6.     public Camera cam;
    7.  
    8.     public GameObject player;
    9.  
    10.     public bool inGame; // change preference
    11.     public bool key87; // w
    12.     public bool key65; // a
    13.     public bool key83; // s
    14.     public bool key68; // d
    15.  
    16.     public float xRotation;
    17.     public float yRotation;
    18.     public float sensitivity;
    19.     public float horizontal;
    20.     public float vertical;
    21.  
    22.     public Rigidbody body;
    23.  
    24.     public CharacterController control;
    25. }
    Code (CSharp):
    1. using UnityEngine;
    2. using System.Collections;
    3.  
    4. public class KeyBinding : MonoBehaviour
    5. {
    6.     private PlayerData gameData;
    7.  
    8.     void UpdateKeyBindingSystem ()
    9.     {
    10.         gameData.key87 = true;
    11.     }
    12.  
    13.     void LateUpdate ()
    14.     {
    15.         UpdateKeyBindingSystem ();
    16.     }
    17. }
    It says: "NullReferenceException: Object reference is not set to an instance of an object. KeyBinding.UpdateKeyBindingSystem () (at Assets/Scripts/KeyBindings.cs:10)"
     
    Last edited: Jul 6, 2015
  20. Shonysky

    Shonysky

    Joined:
    Mar 29, 2014
    Posts:
    44
    Please, someone?
     
    SubZeroGaming likes this.
  21. blizzy

    blizzy

    Joined:
    Apr 27, 2014
    Posts:
    775
    You need to set KeyBinding.gameData somewhere.
     
  22. SubZeroGaming

    SubZeroGaming

    Joined:
    Mar 4, 2013
    Posts:
    1,008
    Watch my tutorial on GetComponent in my misc. content series. You need to create a handle to the script (component) you want, and then reference that handle to it. You're getting a null reference because the handle isn't assigned properly.
     
  23. Shonysky

    Shonysky

    Joined:
    Mar 29, 2014
    Posts:
    44
    It still saying that i can't declare a variable inside a function
     
  24. LaneFox

    LaneFox

    Joined:
    Jun 29, 2011
    Posts:
    7,534
    Do you understand the concept of what has been recommended here?
     
    SubZeroGaming likes this.
  25. SubZeroGaming

    SubZeroGaming

    Joined:
    Mar 4, 2013
    Posts:
    1,008
    Right. Variables go inside the class before your functions.

    public class MyClass{
    public int myVar;

    void MyFunc(){}
    }
     
  26. Shonysky

    Shonysky

    Joined:
    Mar 29, 2014
    Posts:
    44
    Fortunately, after the incompetence of SubZeroGaming, i found my own way to develop what i was looking for.
    Thanks.
     
  27. SubZeroGaming

    SubZeroGaming

    Joined:
    Mar 4, 2013
    Posts:
    1,008
    Ouch. Good luck receiving help in the future.
     
    rightdroid and LaneFox like this.
  28. Shonysky

    Shonysky

    Joined:
    Mar 29, 2014
    Posts:
    44
    No problem, thanks!
     
  29. GroZZleR

    GroZZleR

    Joined:
    Feb 1, 2015
    Posts:
    3,201
    Based on the code you've posted here and your thread history, the only incompetent person here is you. You should be more respectful to people who try to help you learn and grow, but I hope in the future no one wastes their time.
     
  30. visionman50

    visionman50

    Joined:
    Dec 19, 2017
    Posts:
    40
    hi. well. have a few scripts, and public static, and the others with mono behavior. unity builds my game and got no errors in visual studio 2017. but for some stupid reason, it does not build the game into a exe file, and data folder and mono folder. what am i doing wrong. any one able to help. could zip up the project, then upload it to sendspace, then some one could help me out. tried googling, but frustrated. thanks.
     
  31. visionman50

    visionman50

    Joined:
    Dec 19, 2017
    Posts:
    40
    h,hi. took out the public static and then put in the mono behavior. no exe folder. or file, no built. frustrated..