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. Dismiss Notice

money code for my Tower Defense game

Discussion in 'Scripting' started by Sabrinedu1993, Oct 4, 2016.

  1. Sabrinedu1993

    Sabrinedu1993

    Joined:
    Sep 24, 2016
    Posts:
    34
    Hi unity users,
    I want some help to write a script for my tower defense game that when an ennemy is killed my money raise up
    so there is my ennemy code

    Code (CSharp):
    1. using UnityEngine;
    2. public class enemy : MonoBehaviour {
    3.     public float speed = 10f;
    4.     private Transform target;
    5.     private int wavepointIndex = 0;
    6.     void Start()
    7.     {
    8.         target = waypoint.points[0];
    9.     }
    10.     void Update()
    11.     {
    12.         Vector3 dir = target.position - transform.position;
    13.         transform.Translate(dir.normalized * speed * Time.deltaTime, Space.World);
    14.         if (Vector3.Distance(transform.position, target.position) <= 0.4f)
    15.         {
    16.             GetNextWaypoint();
    17.         }
    18.     }
    19.     void GetNextWaypoint()
    20.     {
    21.         if (wavepointIndex >= waypoint.points.Length - 1)
    22.         {
    23.             Destroy(gameObject);
    24.           //  PlayerStats.Money=PlayerStats.Money+1;
    25.             return;
    26.         }
    27.         wavepointIndex++;
    28.         target = waypoint.points[wavepointIndex];
    29.     }
    30. }
    so what i commented is the line that i tryed to write for that i have a script calle PlayerStats wich contain a static variable Money

    Code (CSharp):
    1. using UnityEngine;
    2. using System.Collections;
    3.  
    4. public class PlayerStats : MonoBehaviour {
    5.  
    6.     public static int Money=1000;
    7.     public int StartMoney;
    8.     void start()
    9.     {
    10.         Money = StartMoney;
    11.     }
    12. }
    13.  
    and thanks
     
  2. bigmisterb

    bigmisterb

    Joined:
    Nov 6, 2010
    Posts:
    4,221
    So, what is the question, it looks like this should work. Money is static in the PlayerStats.

    I would use something like this....

    Code (csharp):
    1. PlayerStats.Money += 1;
    I am sure that is not what you are looking for.
     
    takatok likes this.
  3. SubZeroGaming

    SubZeroGaming

    Joined:
    Mar 4, 2013
    Posts:
    1,008
    look into GetComponent to access variables from other scripts.
     
  4. takatok

    takatok

    Joined:
    Aug 18, 2016
    Posts:
    1,496
    Your enemy script needs access to the Player. Tag the object that has PlayerStats script on it with a tag called "PlayerStats" (this is any name you want, but PlayerStats seems to make the most sense).

    Change your code around start to this, I've included some lines you already have for reference
    Code (CSharp):
    1.   private int wavepointIndex = 0;
    2.   PlayerStats playerStats;
    3.     void Start()
    4.     {
    5.         // This gets the Game Object we need that has the tag
    6.         GameObject playerStatObject = GameObject.FindObjectWithTag("PlayerStats");
    7.        // this line gets the actual Script Component we wrote
    8.        playerStats = playerStatObject.GetComponent<PlayerStats>();
    9.  
    10.         target = waypoint.points[0];
    11.     }
    Then you can adjust the money:
    Code (CSharp):
    1. if (wavepointIndex >= waypoint.points.Length - 1)
    2.         {
    3.             Destroy(gameObject);
    4.             playerStats.Money+=1;
    5.             return;
    6.         }
     
  5. Kalladystine

    Kalladystine

    Joined:
    Jan 12, 2015
    Posts:
    227
    It works for me too. Since the field is public static, you don't need a reference to the object (tested without even having it in the scene, just in case - works).

    For clarity's sake I'd move the Destroy command somewhere else (at least after all statements, just before return, preferably to a separate method, but that's outside the topic), but that doesn't change anything here. Not sure what the question is about.
     
    takatok likes this.
  6. takatok

    takatok

    Joined:
    Aug 18, 2016
    Posts:
    1,496
    Didn't catch me made it static
     
  7. Sabrinedu1993

    Sabrinedu1993

    Joined:
    Sep 24, 2016
    Posts:
    34
    So what i have done now i have done a collider for my enemy and my 2 arms(Bullet and Missile) i have tagged them and i have add this function to my arm(a script for my two arms)

    Code (CSharp):
    1.  void OnCollisionEnter(Collision other)
    2.     {
    3.         if (other.gameObject.tag == "Enemy")
    4.         {
    5.             PlayerStats.Money += 10;
    6.             Destroy(gameObject);
    7.         }
    I have a problem in the script that gives the value of the money

    Code (CSharp):
    1. using UnityEngine;
    2. using UnityEngine.UI;
    3. using System.Collections;
    4.  
    5. public class PlayerStats : MonoBehaviour {
    6.     public Text text;
    7.     public static int Money;
    8.     public int StartMoney ;
    9.     void Start()
    10.     {
    11.         Money = StartMoney+1000;
    12.         text = gameObject.GetComponent<Text>();
    13.      
    14.     }
    15.     void Update()
    16.     {
    17.         text.text = ("" + Money);
    18.     }
    19. }
    20.  
    i have an error on the Update function if someone can correct my work
     
  8. Kalladystine

    Kalladystine

    Joined:
    Jan 12, 2015
    Posts:
    227
    What is the error (post it in full)?
    If there's no console error, what's the expected behaviour and what are you getting instead?

    Sorry if that sounds a little rude, but you need to be specific, we don't know what are you trying to do in detail and recreating the scene just to figure out what you might be trying to do is not really going to fly with most people.

    Nevertheless, I've repro'd the scene and it works as expected (there could be some changes done to not update the text every frame, but either way it works), so we're back to square one :)
     
  9. Sabrinedu1993

    Sabrinedu1993

    Joined:
    Sep 24, 2016
    Posts:
    34
    the e
    the error message is

    NullReferenceException: Object reference not set to an instance of an object
    PlayerStats.Update () (at Assets/scripts/PlayerStats.cs:18)
    in this script i want to add 10$ for every ennemy killed
    Ithink there is problem (check the picture that i have uploaded)
    why i have Money Text and not moneyText like i have in my moneyUI script

    Code (CSharp):
    1. using UnityEngine;
    2. using UnityEngine.UI;
    3. using System.Collections;
    4.  
    5. public class MoneyUi : MonoBehaviour {
    6.     public Text moneyText;
    7.     void Update () {
    8.         moneyText.text = "$" +( PlayerStats.Money).ToString();
    9.      
    10.     }
    11. }
    12.  
     

    Attached Files:

    • p.PNG
      p.PNG
      File size:
      310.6 KB
      Views:
      796