Jump to content
  • Advertisement
Sign in to follow this  
Vyil

Some questions for unity+C#

This topic is 708 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Hey,

 

I'm going to start my first project, a text based game like , A Dark Room.

 

I have a few questions regarding the way to program it/set it up.

 

1. In unity, i want to have clickable text, when the player clicks it, it loads to the next level.

I tried with 

void Update () {
        if (Input.GetMouseButtonDown(0)) ;
        Debug.Log("Pressed left mouse.");
 
}
 
But it keeps spamming "Pressed left mouse", and i'm not sure how i even attach the mouse press to the text. Or can i add a button component to the text?
 
2. Ill be cycling through text, and picking up items, should i do it all in new scenes? And just use a LevelManager script? Or can i do multiple levels/texts in 1 scene?
 
3. Where do i start with players being able to save their progress? Or use autosave? For where they are at a level, or the items they picked up.
 
I'll probably have more questions later on , but these are already my first few mindbreakers :)
 

Share this post


Link to post
Share on other sites
Advertisement

The function GetMouseButtonDown() returns true as long as the button is down, which may be an instant or may be many seconds. If the user clicks very quickly you might not happen to probe the button state during that moment. Use it for long term button holding.  EDIT: Sorry, should have double-checked the docs as this is For Beginners and getting things right is important..  There is the similarly named GetMouseButton() that returns true as long as the mouse button is held.

 

If you're looking for a click event, try OnClick().
 

 

For item 2, consider either replacing the text directly if it is actual text, or possibly creating several assets and using SetActive() to turn on and off the ones you need. Using complete levels and adding scenes to your world is a slow and major operation.

 

For item 3, the pattern we've used on the games I've worked on is to load a front-end scene with menus and such, which in turn selects the data to load. Then you need a way to load and save your data, such as the one described here in their official training videos.

Edited by frob
Oops. Thanks ShadowIsADog for pointing out my error.

Share this post


Link to post
Share on other sites
For saving stuff, you can start with PlayerPrefs. It is the simplest way to store data, if all you need is key-value pairs.

Share this post


Link to post
Share on other sites

Your code has a bug in it:

void Update () {
        if (Input.GetMouseButtonDown(0)) ;
        Debug.Log("Pressed left mouse.");

}
 
The if statement here does not work because if statements never have semi-colons after them. So Debug.Log is getting called on every update.
 
If you removed the semi-colon then you could fix the issue. Even if it is a single line I always put brackets around the if statements.
void Update ()
{
    if (Input.GetMouseButtonDown(0))
    {
        Debug.Log("Pressed left mouse.");
    }
}

In Unity : https://docs.unity3d.com/ScriptReference/Input.GetMouseButtonDown.html

 

The documentation says:

 

"You need to call this function from the Update function, since the state gets reset each frame. It will not return true until the user has released the mouse button and pressed it again. button values are 0 for left button, 1 for right button, 2 for the middle button."

 

Here is a tutorial from Unity on persistent data and serialization: https://unity3d.com/learn/tutorials/topics/scripting/persistence-saving-and-loading-data

 

PlayerPrefs is an option as was mentioned. The documentation is here: https://docs.unity3d.com/ScriptReference/PlayerPrefs.html . This is simpler, but I am not crazy about the:

 

"On Windows, PlayerPrefs are stored in the registry under HKCU\Software\[company name]\[product name] key, where company and product names are the names set up in Project Settings."

 

I personally don't really like games that have to mess around with my registry. Also I might be wrong, but it seems like player prefs if they are being stored in the registry with just a project name key are not going to be able to support multiple save files or the ability to migrate/back up the save files easily.

 

Personally I would probably be more inclined to use JSON serialization: https://docs.unity3d.com/Manual/JSONSerialization.html  Although binary serialization can have a smaller file size at the expense of human readability (which for a save file you might not want it to be human readable anyway).

Edited by shadowisadog

Share this post


Link to post
Share on other sites

Additionally, GetMouseButtonUp is the traditional way that UI tends to work.  For example, all the buttons on this forum work only on the mouse button being released, not pressed.  Granted OnClick will handle this for you.

Share this post


Link to post
Share on other sites

If you want to get technical, a click is traditionally only registered when the mouse goes down on a widget and then is on the same widget when it goes up. Browser buttons are actually a bit more strict in this regard depending on how they're implemented. For example, if the button goes down over the text on the button and you move the mouse you start dragging the link and you lose the button activation.

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!