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

i have a problem

Discussion in 'Scripting' started by mr-ddys, Apr 2, 2021.

  1. mr-ddys

    mr-ddys

    Joined:
    Feb 4, 2021
    Posts:
    38
    I've just made a score for my game, and I've tried to do that when you get to a certain score you'll get to the next level, I haven't done the next level code yet but it's irrelevant at the moment. I'm having a CS0029 error, line 31. Please help me.
    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4. using UnityEngine.UI;
    5. using UnityEngine.SceneManagement;
    6.  
    7. public class scoreactive : MonoBehaviour
    8. {
    9.  
    10.     public Text MyscoreText;
    11.     private int ScoreNum;
    12.     public int ScoreTarget;
    13.    
    14.  
    15.     // Start is called before the first frame update
    16.     void Start()
    17.     {
    18.         ScoreNum = 0;
    19.         MyscoreText.text = "" + ScoreNum;
    20.     }
    21.  
    22.    
    23. private void OnTriggerEnter2D(Collider2D Hexegon)  
    24.     {
    25. ScoreNum += 1;
    26. MyscoreText.text = "" + ScoreNum;
    27.      }
    28.  
    29. void FixedUpdate()
    30. {
    31. if(MyscoreText.text = ScoreTarget)
    32. {
    33.  
    34. }
    35. }
    36. }
    37.  
     
  2. Joe-Censored

    Joe-Censored

    Joined:
    Mar 26, 2013
    Posts:
    11,847
    How many error codes have you memorized? I've memorized exactly 0. The error has a description which usually states exactly what is wrong, as well as the exact line number. That's the important information.
     
    Kurt-Dekker likes this.
  3. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    36,336
    Joe-Censored likes this.
  4. Hikiko66

    Hikiko66

    Joined:
    May 5, 2013
    Posts:
    1,300
    Code (csharp):
    1. if(MyscoreText.text = ScoreTarget)
    = is for assignment
    == is for comparison

    ScoreTarget is an int.
    MyscoreText.text is a string.

    Off hand I don't know if it will convert an int into a string automatically for comparison.

    If not, you can convert the int to a string for the comparison with ScoreTarget.ToString()
     
    Last edited: Apr 2, 2021
    Joe-Censored likes this.
  5. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    36,336
    All of this is true... but also it might not be appropriate to compare for a precise score. In basketball you can earn 2 or 3 points, so it is possible to reach 99 points and if your goal was 100 points, you could never reach it.

    Usually the tradition is to compare the integer value of the score to the integer goal, checking if it is greater than or equal to.

    But perhaps your case requires a precise score to be attained, I dunno.
     
    Joe-Censored likes this.
  6. Hikiko66

    Hikiko66

    Joined:
    May 5, 2013
    Posts:
    1,300
    Yeah. And I'm not sure why he is comparing ScoreTarget to MyscoreText when he could just compare it to ScoreNum, which is actually the int that MyscoreText gets its value from.
     
    Joe-Censored likes this.
  7. mr-ddys

    mr-ddys

    Joined:
    Feb 4, 2021
    Posts:
    38
    The reason I did it that way, because I wanted it to be easier to create stages, and I wouldn't have to create a new script every time.
     
  8. mr-ddys

    mr-ddys

    Joined:
    Feb 4, 2021
    Posts:
    38
    I initially tried to do it this way, but it didn't work.
     
  9. mr-ddys

    mr-ddys

    Joined:
    Feb 4, 2021
    Posts:
    38
    I still can't fix it.
     
  10. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    36,336
    If you are unable to verbalize better than "can't fix it," it sounds like perhaps you want to slow down, back up and perhaps study some other basic Youtube game tutorials. A score tracker is basically the simplest common thing that every game with a score and a target goal score requires.
     
    Joe-Censored likes this.
  11. mr-ddys

    mr-ddys

    Joined:
    Feb 4, 2021
    Posts:
    38
    I fixed it, thanks.