Jump to content
Search In
  • More options...
Find results that contain...
Find results in...
Quantzer

Please help with a very simple video game

Recommended Posts

Posted · Original PosterOP

Hello, I am programming a game for a school project. I have just dove in to the C# and Unity. My problem is transfering score(basically players position) from the level scene to the menu. So far I got this. Be aware that I am very unfamiliar with C# commands and its syntax.

using UnityEngine;

public static class StaticScrScr : MonoBehaviour {

    private static int score;

    public static int Scoreset(sc)

## not sure how to properly call this method
    {
        set	this se as well as get seem out of place but its what I found on the internet. I assume that it is just a general code explanation but I don't know what am I supposed to put instead of them.
        {
            score = sc;
        }
    }

    public static int Scoreget()
    {
        get
## this se as well as get seem out of place but its what I found on the internet. I assume that it is just a general code explanation but I don't know what am I supposed to put instead of them.
        {
            return score;
        }
        
    }
}

This is my static script its where I save score value.

using UnityEngine;
using UnityEngine.UI;

public class Score : MonoBehaviour {

    public Transform player;
    public Text scoretxt;
    private float poz;

    // Update is called once per frame
    void Update () {
        poz = player.position.z.ToString("0");
        scoretxt.text = poz;
        StaticScrScr.Scoreset(poz);
    }
}

This i my score script where I update score on game scene in real time and I save it to the static script

 

using UnityEngine;
using UnityEngine.UI;

public class updatingscore : MonoBehaviour
{

    public Text scoretxt;

    void Start ()
    {
        scoretxt.text = StaticScrScr.Scoreget();
    }

}

This is a script that I atached to the text on game menu that should draw the score value from static script and change text whenever the menu is loaded.

 

In my High school we are programming in python but this is for my final Assigment. So I am aware of how some things in coding work but am very unfamiliar with both C# and unity.

Link to post
Share on other sites
Posted (edited)
22 minutes ago, Quantzer said:

-snip-

Ah, where I started with C#, this will be fun, alright...

First code block, use score as a parameter... as you seem to be trying to do, but its not quite right, there arent any functions needed, its like this:
 

private int score; // Create a variable to store the value
public int Score { // Create the parameter
  get {
    return score; // if we want to get the value of Score, just return the variable we made earlier (the private one)
  }
  set {
    score = value; // value is a C# keyword, it basically just is used so that when you say Score = whatever; it runs this code, and value is replaced with whatever
  }
}

2nd code block, a few things:
1. From this im guessing you are making a game where you jump up as far as you can go without falling?
2. you dont need anything in the ToString() method, just leave the inside of those parenthesis blank

3. add all the classes to a namespace so you can access them from each other, its also good practice for when you work with others

4. You dont need to have all that running in Update, just make some events that if the player moves up, have the height counter update, and show the new value on the text box you are using, dont do this for when they fall and just label the text box "best height" or something like that, then add an event for when they start falling, set the score based on the highest height reached so far.

 

3rd code block, just use the parameter properly, everything else is fine, also put it in a namespace

 

EDIT: Almost forgot, it would help a lot more if you told is what error you are getting... all I was able to do was correct mistakes based on guesses as to what you are doing, and just correcting bad practice, cant really do much more without more info!

Edited by JustMATT
Forgot something

"Every program needs 2 things: 1: A dark theme... and 2: A 'Fuck off!' button, no exceptions!" -Me

Link to post
Share on other sites
Posted · Original PosterOP

Thanks man I kinda got to where I wanted to. This helps I'll try to do fiddle with it a bit more over the summer.

Yeah...I forgot to mention, but you were on the right track. It is supposed to be a infinite runner not jumper(;

Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Announcements


×