Search Unity

Add score to player, after touch coin (C#)

Discussion in 'Scripting' started by Varaosa, Sep 24, 2014.

  1. Varaosa

    Varaosa

    Joined:
    Sep 5, 2014
    Posts:
    10
    Hi, Unity Community! :3

    I like to ask this time, how i can call code/thinks to another object code & how to use or call (other or self) inspector thinks into code?

    But, this is the problem anyway:

    I have script in "Main Camera" -what have inside also "Hud interface" -Scirpt hud element & what have score system inside & that show/are visible for player.

    Also i have "coin(s)", what have a collision. When player touch that, that destroy self, but not have a value (score) to add score into hud element, so...

    Ther is code(s) what i use:

    Hud interface(camera):
    Code (CSharp):
    1. public int Coins = 0;
    and also

    Code (CSharp):
    1. GUI.Label(new Rect(Screen.width / 2 - *,*,*,*), "Coins: " + Coins.ToString());
    Into coin(s) i use this code only:

    Code (CSharp):
    1.  void OnTriggerEnter(Collider Player){
    2.             Destroy(gameObject);
    3.     }
    -Into coins there is no "value" -for coin & "camera" -not have player collision, because camera not gonna touch player & player code not have coin or hud script.

    So how i can add score for player after touch coin when camera & coin script was separate?

    What would be a good solution for this?

    Thanks! :3
     
    Last edited: Sep 24, 2014
  2. Krysalgir

    Krysalgir

    Joined:
    Aug 30, 2010
    Posts:
    95
    a simple (but quite dirty) way could be
    Code (csharp):
    1.  
    2. // in Interface.cs
    3. public static int Coins = 0;
    4.  
    5. // in coin.cs
    6. void OnTriggerEnter(Collider Player) {
    7.     // Maybe check that the Collider is indeed the player...
    8.  
    9.     Interface.Coins++;
    10.     Destroy(gameObject);
    11. }
     
    Varaosa likes this.
  3. XeOniFiCaTiOn

    XeOniFiCaTiOn

    Joined:
    May 7, 2013
    Posts:
    17
    yeah . Your better off creating a static storage class that has to store things like score, player name etc. Always remember to flush static variables like the score on reset of level.

    for the coin. Make a coin class with the OnTriggerEnter function to increase the score and attach it to your coin 3d or 2d asset. Make a prefab out of it for further use.

    regards
     
    Varaosa likes this.
  4. Krysalgir

    Krysalgir

    Joined:
    Aug 30, 2010
    Posts:
    95
    I can also advice to have some LevelManager singleton that manages all things that are purely "local" to a single level.
    To store things throughout the entire game session, you can have a GameManager/PlayerManager singleton(s) that don;t get destroyed on load...
     
    Varaosa likes this.
  5. Limeoats

    Limeoats

    Joined:
    Aug 6, 2014
    Posts:
    104
    This is exactly the way to handle this. Have a singleton LevelManager to handle these types of things. That way, you could simply do something like this:

    Code (CSharp):
    1. public void OnTriggerEnter(Collider other) {
    2.     //check to make sure the object touching the coin is a player
    3.     if (other.GetComponent<Player>() != null) {
    4.         LevelManager.Instance.AddCoins(1);
    5.     }
    6. }
     
    Varaosa likes this.
  6. Varaosa

    Varaosa

    Joined:
    Sep 5, 2014
    Posts:
    10
    That work for me, but i found a bug now. Looks like there is someting problem meaby into "coin" collision & after game restart debug message say, even i don't touch coin, then im all ready to touch for that & after restart the game, game add automatic 1 coin into hud/interface & that game level coin was gone.

    I can use this into "coin" -script:
    Code (CSharp):
    1. void OnTriggerEnter(Collider Player) {
    2. Interface.Coins++;
    3. Destroy(gameObject);
    4. }
    5.  
    6. //Meaby need "TriggerExit" -too, if collision not work?
    7.  
    8. /* void OnTriggerExit(Collider Player){
    9. Interface.Coins++;
    10. Destroy(gameObject);
    11. }*/
    or also but those " Interface.Coins++;" -on that, but i can't reset score after reset level & also i want destroy "coin" game object immediately after contact, so this is not work for me.

    I can also add some collaider also into player:
    Code (CSharp):
    1. void OnTriggerEnter(Collider gameObject.name == "coin") {
    2. ...}
    But unity say:
    Code (CSharp):
    1. error CS0111: A member `Charther.OnTriggerEnter(UnityEngine.Collider)' is already defined. Rename this member or use different parameter types
    -So unity don't give me create new collaider named "coin", because collaider is already created.

    I can fix that one way, but i need reset my static "Coins" value to 0 after restart level into main menu- /restart level button, but that "public static" not working inside "void{}".

    Code (CSharp):
    1. public static int Coins = 0;
    Anotherwise i like to know, how to save settings/game static if player close the game? When player start game again game can load static/score again after game restart. How to do that?

    I can try that LevelManager also into future if i need that. Thanks! :3
     
    Last edited: Sep 26, 2014
  7. Varaosa

    Varaosa

    Joined:
    Sep 5, 2014
    Posts:
    10
    Okey, i fix that coin/collision problem to add this script into player:
    Code (CSharp):
    1. if(Player.gameObject.name == "Coin")
    2.         {
    3.             Interface.Coins++;
    4.             Destroy(Player.gameObject);
    5.         }
    -But, i can't jet reset static coin/score if game restarted into "menu" -way.
    I need reset someway my "public static int" -number or reset my "GUI.label".

    Thanks! :3
     
    Last edited: Sep 26, 2014
  8. Varaosa

    Varaosa

    Joined:
    Sep 5, 2014
    Posts:
    10
    -Okey, i fixed that now & status can reset now after new game. Still i have couple little problems what im ask this therd before.

    But, now i want to know, how i can load status if scene change? If player was collect some coins into game level & when player change level that have also that much coins after new level.

    Also i like to know, if player buy someting, then game can check if player have a coin "XX", then player can buy that, anotherwise not.

    I have this kind of code for coin check:

    Code (CSharp):
    1. if (Coins >= 1)
    2. {
    3. do someting...
    4. }
    5. else
    6. {
    7. noting happends...
    8. }
    I like to ask also this time, how i can call code/thinks to another object code & how to use or call (other or self) inspector thinks into code? Just for future... :3
     
    Last edited: Oct 2, 2014
  9. Varaosa

    Varaosa

    Joined:
    Sep 5, 2014
    Posts:
    10
    Okey, i fix that coin checking system now. :3
     
  10. Vipsu

    Vipsu

    Joined:
    Oct 8, 2012
    Posts:
    88
    Whatever you do, rename your interface.cs class to something more appropriate. Naming your Class Interface is as bad as naming your class Class.cs. This is something that is a big No! No!

    Interface keyword is used to by C# (and many other programming languages) to define a interface
    http://msdn.microsoft.com/en-us/library/87d83y5b.aspx

    Often the object that handles Gamelogic related to scoring, win/lose conditions and everything related to those is called GameManager.
     
    Varaosa likes this.
  11. Limeoats

    Limeoats

    Joined:
    Aug 6, 2014
    Posts:
    104
    Agreed. For @Varaosa 's reference, I use GameManager to handle the accumulated "Points" for my current game, and I use a LevelManager class to deal with things like checkpoints, the level beginning, etc. It is good practice to use these names since they are both very intuitive and they make sense to others when you ask for help.
     
    Varaosa likes this.
  12. Varaosa

    Varaosa

    Joined:
    Sep 5, 2014
    Posts:
    10
    I don't know or much sure about, how to use GameManager or LevelManager. I don't use any unity assets or plugins. I try just code my self or use tools what i have already into unity.
     
  13. Limeoats

    Limeoats

    Joined:
    Aug 6, 2014
    Posts:
    104
    I can help you get started with the LevelManager. This is just the basic idea behind it:

    Code (CSharp):
    1. class LevelManager : MonoBehavior {
    2.   public static LevelManager Instance { get; set; }
    3.   public Checkpoint[] Checkpoints;
    4. }
    That's all there is behind a singleton class like LevelManager. Any time you want to work with something in your level manager from another script, simply say this:

    Code (CSharp):
    1. public class Player {
    2.   void Awake() {
    3.     this.transform.position = LevelManager.Instance.Checkpoints[0].transform.position;
    4.   }
    5. }
    In order to make the LevelManager work, you need to attach it to an empty game object.
     
    Last edited: Oct 6, 2014
    Varaosa likes this.
  14. Varaosa

    Varaosa

    Joined:
    Sep 5, 2014
    Posts:
    10
    I try that, thanks! :3
     
  15. Limeoats

    Limeoats

    Joined:
    Aug 6, 2014
    Posts:
    104
    Sure, no problem. The GameManager has the same exact idea behind it, except you just use it for a different purpose. Setting it up as a singleton is exactly the same, however.
     
  16. tarun9689

    tarun9689

    Joined:
    Mar 31, 2016
    Posts:
    1
    hi I am Tarun Sharma..............
    regarding this i also have query..............when my player is colliding with the coin(multiple),the coins are getting destroye.......but whenever i restart my game on android only last coin got reappear on screen rest are set Active False..........
    what to do