Search Unity

  1. Unity 6 Preview is now available. To find out what's new, have a look at our Unity 6 Preview blog post.
    Dismiss Notice
  2. Unity is excited to announce that we will be collaborating with TheXPlace for a summer game jam from June 13 - June 19. Learn more.
    Dismiss Notice
  3. Dismiss Notice

Resolved My system of money don't function, i need help please

Discussion in 'Scripting' started by Zazou67, May 22, 2024.

  1. Zazou67

    Zazou67

    Joined:
    Nov 24, 2023
    Posts:
    38
    Hi, i creat a video games for me, and when kill a enemy, i want win 50 coin, but my system's obsolet (because, i don't know how doing that :eek:), you can help me please :(:D
     
  2. lordofduct

    lordofduct

    Joined:
    Oct 3, 2011
    Posts:
    8,594
    Gonna need more to go on.

    What do you mean your system's obsolete? Show us your code? Tell us what's not working? Tell us how you expect it to work?
     
  3. Zazou67

    Zazou67

    Joined:
    Nov 24, 2023
    Posts:
    38
    I want of my enemy give 50 coin when he's kill, i cherche my code and when i teld "system obsolet" it's of he's don't function :confused:
     
  4. Zazou67

    Zazou67

    Joined:
    Nov 24, 2023
    Posts:
    38
    Player :
    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4.  
    5. public class PlayerLife : MonoBehaviour
    6. {
    7.     public float LifePlayer;
    8.     public float effectDisplayTime;
    9.     public GameObject effectsPrefab;
    10.     public float Money;
    11.  
    12.     void Update()
    13.     {
    14.         if (Input.GetKeyUp(KeyCode.Escape))
    15.         {
    16.             LifePlayer -= 5;
    17.         }
    18.         if (LifePlayer <= 0)
    19.         {
    20.             Die();
    21.         }
    22.     }
    23.  
    24.     public void TakeDammagePlayer(float amount)
    25.     {
    26.         //LifePlayer -= amount;
    27. //        if (LifePlayer <= 0)
    28.         {
    29.             Die();
    30.         }
    31.     }
    32.  
    33.     private void Die()
    34.     {
    35.         GameObject effect = Instantiate(effectsPrefab, transform.position, Quaternion.identity);
    36.         Destroy(effect, effectDisplayTime);
    37.         Destroy(gameObject);
    38.     }
    39. }
    40.  
    Enemy :
    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEditor;
    4. using UnityEngine;
    5. using UnityEngine.AI;
    6.  
    7. public class EnemyLife : MonoBehaviour
    8. {
    9.     public float Life = 100f;
    10.     public float DamageEnemy;
    11.     [SerializeField] private GameObject effectsPrefab;
    12.     [SerializeField] private float effectDisplayTime = 3.0f;
    13.     public NavMeshAgent enemy;
    14.     public GameObject player;
    15.     public float ValueMoney;
    16.  
    17.  
    18.     void FixedUpdate()
    19.     {
    20.        
    21.     }
    22.  
    23.  
    24.     void Update()
    25.     {
    26.        
    27.            enemy.SetDestination(player.transform.position);
    28.        
    29.     }
    30.  
    31.     public void TakeDammageEnemy(float amount)
    32.     {
    33.         Life -= amount;
    34.         if (Life <= 0)
    35.         {
    36.             Die();
    37.         }
    38.     }
    39.  
    40.     void EnemyDamage(Transform player)
    41.     {
    42.         PlayerLife p = player.GetComponent<PlayerLife>();
    43.         if (p != null)
    44.         {
    45.             p.TakeDammagePlayer(DamageEnemy);
    46.         }
    47.         else
    48.         {
    49.             Debug.LogError("Pas de script Enemy sur l'ennemi");
    50.         }
    51.     }
    52.  
    53.     private void Die()
    54.     {
    55.         GameObject effect = Instantiate(effectsPrefab, transform.position, Quaternion.identity);
    56.         Destroy(effect, effectDisplayTime);
    57.         //it's here i want doing my code for the money
    58.         Destroy(gameObject);
    59.     }
    60. }
    61.  
     
  5. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    39,357
    Assuming that:

    - each of your enemies has a valid
    player
    reference filled out (IMPORTANT)
    - each of your enemies has their
    ValueMoney
    filled out properly
    - assuming this Die() method is called when the enemy dies...

    Then you can simply say:

    Code (csharp):
    1. player.Money = player.Money + ValueMoney;
    But for displaying it, that's a lot more complicated. I suggest starting with any one of the millions of Youtube tutorials already out there, and work like this guy:

    Imphenzia: How Did I Learn To Make Games:




    If you run into trouble, then it is time to start debugging!

    By debugging you can find out exactly what your program is doing so you can fix it.

    https://docs.unity3d.com/Manual/ManagedCodeDebugging.html

    Use the above techniques to get the information you need in order to reason about what the problem is.

    You can also use
    Debug.Log(...);
    statements to find out if any of your code is even running. Don't assume it is.

    Once you understand what the problem is, you may begin to reason about a solution to the problem.
     
    Bunny83 and Zazou67 like this.
  6. Zazou67

    Zazou67

    Joined:
    Nov 24, 2023
    Posts:
    38
    Great thanks, i go test that now ! i teld you if your reply's good for me, and thanks for your help and your time :D:rolleyes::)
     
  7. Zazou67

    Zazou67

    Joined:
    Nov 24, 2023
    Posts:
    38
    And for Debuging, i know how doing that, very thanks agian:D:D:D:D:D
     
  8. Zazou67

    Zazou67

    Joined:
    Nov 24, 2023
    Posts:
    38
    upload_2024-5-22_19-2-51.png
    i'v seen a error in my code, it's normal ? (sorry, i'm very novice in unity 3D)
     
  9. Zazou67

    Zazou67

    Joined:
    Nov 24, 2023
    Posts:
    38
    i'v corrected of that :
    upload_2024-5-22_19-5-28.png
    i don't know if it's good or not
     
  10. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    39,357
    Oh very nice! I made a mistake!!! You fixed my mistake.

    When I read your code I thought that
    player
    was a
    PlayerLife
    but now I see it was a
    GameObject
    .

    If it works then it is good! Does it work?
     
    Bunny83 and Zazou67 like this.
  11. Zazou67

    Zazou67

    Joined:
    Nov 24, 2023
    Posts:
    38
    yes it's perfect, the code is very simple and efficient:D:oops:
     
    Kurt-Dekker likes this.
  12. Zazou67

    Zazou67

    Joined:
    Nov 24, 2023
    Posts:
    38
    but i've other problem, it's with my nev mesh...:( (thanks again for befor:oops:)
     
  13. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    39,357
    New problem, new post please!

    Try this pattern:

    How to report your problem productively in the Unity3D forums:

    http://plbm.com/?p=220

    This is the bare minimum of information to report:

    - what you want
    - what you tried
    - what you expected to happen
    - what actually happened, log output, variable values, and especially any errors you see
    - links to actual Unity3D documentation you used to cross-check your work (CRITICAL!!!)

    The purpose of YOU providing links is to make our job easier, while simultaneously showing us that you actually put effort into the process. If you haven't put effort into finding the documentation, why should we bother putting effort into replying?
     
    Zazou67 likes this.
  14. Zazou67

    Zazou67

    Joined:
    Nov 24, 2023
    Posts:
    38
    Oh ok, i'm not very good in english sorry, i'm french, and i'm young (i've not 18 years old), so i'm not very at ease with every that, sorry. But, i note your remarque and i think more seek in a documentation, thanks for your help :D
     
    Kurt-Dekker likes this.
  15. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    39,357
    You are welcome! It feels like you are making excellent effort and progress. All of this will take time to understand.

    I like this approach: one small step at a time.

    Imphenzia: How Did I Learn To Make Games:

     
    Bunny83 and Zazou67 like this.
  16. Zazou67

    Zazou67

    Joined:
    Nov 24, 2023
    Posts:
    38
    Very thanks, it's encouraging, i think retrurn of you if i've a question or bug, you are very friendly with me :D;)
     
    Bunny83 likes this.