Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. Voting for the Unity Awards are OPEN! We’re looking to celebrate creators across games, industry, film, and many more categories. Cast your vote now for all categories
    Dismiss Notice
  3. Dismiss Notice

Public void GmAddScore() is not working properly

Discussion in '2D' started by Ahsan_mughal, Mar 30, 2018.

  1. Ahsan_mughal

    Ahsan_mughal

    Joined:
    Mar 30, 2018
    Posts:
    10
    kindly help me ,,I have done all, but this error prevent me to play my own game, anyone code master kindly help , GmAddScore is not working ,,
    Public Void GmAddScore(){
    this.myScore++;
    }
    and i call it on the obstacles script but unity shows all of the errors abour the same it dont adding the score , and dragon drops down and error generating continously :( :'( please somebody help ,, its about my lots of struggle :(
     

    Attached Files:

  2. Vryken

    Vryken

    Joined:
    Jan 23, 2018
    Posts:
    2,106
    GameObject.Find() searches for a GameObject in the scene that has the name you specify. You can't use it to find individual components like your GameManager script, you instead have to input the name of the GameObject that has the GameManager script attached to it.
     
  3. methos5k

    methos5k

    Joined:
    Aug 3, 2015
    Posts:
    8,712
    It looks as though he's using GetComponent in the call, too.

    This was double posted. OP: Please don't double post.
    I responded to this in 'Scripting', I believe.

    Edit: Correction, sorry.. the post to which I responded was a reply to an older thread (posted as a question*).
     
    Last edited: Mar 31, 2018
  4. HaddicusGames

    HaddicusGames

    Joined:
    May 28, 2014
    Posts:
    28
    Is the name of your Text item "Text" in the Hierarchy window? Whatever the name of the Gameobject is there, is what Unity will search for, in the scene.
     
  5. Ahsan_mughal

    Ahsan_mughal

    Joined:
    Mar 30, 2018
    Posts:
    10
    @HaddicusGames yes text is sub-object of the gameobject convas for UI, To show the public text in the game
     
  6. Ahsan_mughal

    Ahsan_mughal

    Joined:
    Mar 30, 2018
    Posts:
    10
    Bro!!!
    What to do now!
    Should i change the gameobject or in the base Gameobject.Find(),,, individually calling the components is'nt the right option!
     
  7. Ahsan_mughal

    Ahsan_mughal

    Joined:
    Mar 30, 2018
    Posts:
    10
    But gamemanager=Gameojbect.Find("Game Manager").GetComponent<Gamemanager>() ;
    Through it i call
    Void AddScore() {
    gamemanager.GmAddScore();
    }


    Aaaaahhhhh
     
  8. methos5k

    methos5k

    Joined:
    Aug 3, 2015
    Posts:
    8,712
    Well, don't freak out... What is null: the text object or the game manager? It should tell you in the console where the error is and you can double click to open it at that spot.
    Just ensure that whatever is there isn't null. :)
     
  9. Ahsan_mughal

    Ahsan_mughal

    Joined:
    Mar 30, 2018
    Posts:
    10
    @methos5k see this s.s !
     

    Attached Files:

  10. Ahsan_mughal

    Ahsan_mughal

    Joined:
    Mar 30, 2018
    Posts:
    10
    dragon falls down ,,, and down down down down nd so on ... to hell huh !!!!!!
     
  11. methos5k

    methos5k

    Joined:
    Aug 3, 2015
    Posts:
    8,712
    Do you find the game object in Awake?
     
  12. Ahsan_mughal

    Ahsan_mughal

    Joined:
    Mar 30, 2018
    Posts:
    10
    void Start(){
    myScore = 0;
    myScoreGUI = GameObject.Find ("Text").GetComponent<Text> ();
    InvokeRepeating ("ObstacleSpawner", .5f, 1.5f);
    }
    public void GmAddScore(){

    this.myScore++;
    myScoreGUI.text = myScore.ToString();
    }
     
  13. methos5k

    methos5k

    Joined:
    Aug 3, 2015
    Posts:
    8,712
    I meant the game manager being found..
     
  14. Ahsan_mughal

    Ahsan_mughal

    Joined:
    Mar 30, 2018
    Posts:
    10
    NullReferenceException: Object reference not set to an instance of an object
    ObstacleScript.AddScore () (at Assets/Scripts/ObstacleScript.cs:38)
    ObstacleScript.Update () (at Assets/Scripts/ObstacleScript.cs:28)

    this is the error!
    it cannot find the object from gamemanager script and error generated in obstaclescript