Search Unity

can anyone tell me why i am getting this error

Discussion in 'Scripting' started by Prabin12, Jul 7, 2019.

  1. Prabin12

    Prabin12

    Joined:
    Jan 4, 2019
    Posts:
    3
    i am getting this error , what might be wrong in my script;This is a GameManget.cs script

    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4.  
    5. public class GameManager : MonoBehaviour
    6. {
    7. public static GameManager singleton;
    8. public bool GameStarted { get; private set; }
    9. public bool GameEnded { get; private set;}
    10.  
    11.  
    12. // Start is called before the first frame update
    13. private void Awake()
    14. {
    15. if (singleton == null)
    16. {
    17. singleton = this;
    18. }
    19. else if(singleton != this)
    20. {
    21. Destroy(gameobject);
    22. }
    23. }
    24.  
    25. public void StartGame()
    26. {
    27. GameStarted = true;
    28. Debug.Log("Game Started");
    29. }
    30.  
    31. public void EndGame(bool win)
    32. {
    33. GameEnded = true;
    34. Debug.Log("Game Ended");
    35.  
    36. if (!win)
    37. {
    38. //Restart the game
    39. Invoke("RestartGame",2);
    40. }
    41. }
    42.  
    43. public void RestartGame()
    44. {
    45. UnityEngine.SceneManagement.SceneManager.LoadScene(0);
    46. }
    47.  
    48.  
    49. }
    50.  

    And this is the scrpt for my ball controller

    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4.  
    5. public class Ballcontroller : MonoBehaviour
    6. {
    7.  
    8. [SerializeField] private float thrust = 150f;
    9. [SerializeField] private Rigidbody rb;
    10. [SerializeField] private float wallDistance = 5f;
    11. [SerializeField] private float minCamDistance = 3f;
    12.  
    13. private Vector2 lastMousePos;
    14.  
    15. // Update is called once per frame
    16. void Update()
    17. {
    18. Vector2 deltaPos = Vector2.zero;
    19.  
    20. if(Input.GetMouseButton(0))
    21. {
    22. Vector2 currentMousePos = Input.mousePosition;
    23.  
    24. if(lastMousePos == Vector2.zero)
    25. lastMousePos = currentMousePos;
    26.  
    27. deltaPos = currentMousePos - lastMousePos;
    28. lastMousePos = currentMousePos;
    29.  
    30. Vector3 force = new Vector3(deltaPos.x, 0, deltaPos.y) * thrust;
    31. rb.AddForce(force);
    32. }
    33. else
    34. {
    35. lastMousePos = Vector2.zero;
    36. }
    37. }
    38.  
    39. private void FixedUpdate()
    40. {
    41. if (GameManager.singleton.GameEnded)
    42. return;
    43.  
    44. if (GameManager.singleton.GameStarted)
    45. {
    46. rb.MovePosition(transform.position + Vector3.forward * 5 * Time.fixedDeltaTime);
    47.  
    48. }
    49. }
    50.  
    51. private void LateUpdate()
    52. {
    53. Vector3 pos = transform.position;
    54.  
    55. if(transform.position.x < -wallDistance)
    56. {
    57. pos.x = -wallDistance;
    58. }
    59. else if(transform.position.x > wallDistance)
    60. {
    61. pos.x = wallDistance;
    62. }
    63.  
    64. if(transform.position.z < Camera.main.transform.position.z + minCamDistance)
    65. {
    66. pos.z = Camera.main.transform.position.z + minCamDistance;
    67. }
    68. transform.position = pos;
    69. }
    70.  
    71. private void OnCollisionEnter(Collision collision)
    72. {
    73. if(GameManager.singleton.GameEnded)
    74. return;
    75.  
    76. if(collision.gameObject.tag == "Death")
    77. GameManager.singleton.EndGame(false);
    78. }
    79.  
    80. }
     

    Attached Files:

    Last edited: Jul 7, 2019
  2. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    38,726
    Please check the first post in this forum for code formatting guidelines.
     
    SparrowGS likes this.
  3. SparrowGS

    SparrowGS

    Joined:
    Apr 6, 2017
    Posts:
    2,536
    Theres no such thing as "gameobject", MB has a field called "gameObject" that points to the game object the script is attached to.