Hi I am making a fps shooter game with unity 3d and I am trying to add a in game currency system but in order to do this I need to transfer a float called coins that is on my players movement script from the level scene to the store scene in order for it to be spent and displayed on screen as a UI dose anybody know how I can do this thank you so much for your help😁
Do not store variables in scripts where they do not belong. There are multiple solutions to this problem:
Option is to create a ScriptableObject that contains variables that need to be transferred. It is instantiated in the editor by you and then assigned as an asset to your player prefab. Instead of setting a variable in the player itself, the player just sets a variable in this storage.
This storage can be anything, from a single variable (see this video for reference) to big systems like Unitys Tilemap System.
You create a component that is attached to a game object that resides in the "Dont Destroy On Load" scene. This can be done by creating a monobehaviour script and calling DontDestroyOnLoad(gameObject) in its Start method. Additionally, you can make it a singleton and access it from everywhere. For reference see this answer from the game dev stackexchange.
To be honest, I always try to go with the first one. If you want some variables to persist in a "game state" (that's what its called in Unreal), then you create it externally and reference it throughout you session. There are really a lot of situational approaches and there is no real right or wrong, but you should always keep in mind that a class should only serve a single purpose and a player script that moves the player and stores the current stats (that should live longer than the player itself) is usually a bad approach.
Related
First time asking a question. Please be patient.
I'm making a dice roller that keeps track of the player's saves, skills, and etc.
I made a UI game object for the player's weapon that contains two scripts for attack and damage. I pass in the values (int) that the scripts need from textboxes attached to the game object. The player clicks a button to add a new weapon.
I was wondering how I can save the weapons list and the values each weapon have.
Building with Unity for android.
At the moment I saved the weapon roller as a prefab. I was thinking I need to make a weapon class so I can save. I never tried to make a mobile app. I "know" that playerprefs work for android, but I don't think I can use playerprefs to save a game object with values.
Any help to lift my fog of confusion would be appreciated.
I'm actually working on a board game using unity. It's a board game where players have to connect two opposites borders by claiming empty hexagonal cases. Borders are assigned to players at the beginning.
I chose to represent my empty cases with gems, white when unclaimed, and red or blue for players. So I made a single prefab for all gems.
To check connections between gems themselves and with borders, I assigned to them slightly bigger box colliders so they are colliding (trigger) with other objects, so I can detect if a claimed gem is connected (directly or not) to a border. I wrote this code generically as gems are one single prefab, naively thinking that every entity would run its own version of the script.. But that's not the case.
So I'm looking for an alternative way to detect connections with borders, but still generically, as the size of the board is variable.
Thanks in advance !
Your problem is closely related on how you break your game in game objects and components on Unity3D.
First of all, use scripts to define components. For example your gems could have a script that contains data related to its state (your white, red and blue gems meaning empty, playerA and playerB).
You will probably need a game object (normally called a GameManager) to handle all behavior related to game rules like which player is playing, if a player can pick a specific gem and if a player won the game. Depending on the game complexity is a good idea to also have a game object with a component (script) to keep your board state, instead of simply getting it from the gems objects Transform component.
After you have structured your game you do not need colliders to detect connections. Every time you have to check for connections, just iterate over your gems or use your board state object.
Edit:
References to help you develop your game:
How do I represent a hextile/hex grid in memory?
Creating a holder for game level for simple board tile based game.
creating 2d table\chess board\2d array
Currently I have my toggle to On Value Change make AudioSource.enabled, which does turn the music off.
However when I die in the game the level gets reloaded causing the toggle to go back to its default state and then i am unable to turn the music back on.
So what is the best way to make a music toggle and have the state of the toggle be remembered on load?
Another solution would be a singleton music manager game object that is created only once and is persistent between scenes, meaning that it will not be destroyed when you reload a scene or load a different scene.
You can read more about Singleton patterns here: http://unitypatterns.com/singletons/
You can save persistent data across scenes using PlayerPrefs. Just store there the audio status and be sure to check if it's enabled when loading a new scene (for example inside Awake method of a MonoBehaviour attached on the same AudioSource's GameObject).
Side note
Depending on the specific situations, turn off globally the volume could be easier using AudioListener.volume rather than modifying all AudioSources.
I'm making a role playing game in C# using XNA. I already have a map and some stuff, but that's not interesting at the moment. My question is: How can I give the player the possibility to enter houses or rooms?
To create the worlds, I've used standard int-arrays where each number represents a different type of tile. That works all fine, but the house isn't enterable but a solid textured block of something.
BTW I've used a Vector3 to determine in which world the player's currently located and which one the program must load next.
Any suggestions how I can make the houses enterable?
One easy way to make houses enterable is to create a trigger object at the door of the house.
This object can be a simple Rect along with an id.
While the player moves around your map check for a collision between the player and the trigger.
When the player enters this trigger you can change the displayed map with a new one (the interior of the house) and move the player where the door should lead.
If you're using C# to make a game, you might want to consider switching to MonoGame (which is practically XNA's successor since XNA is dead) or Unity.
As mentioned previously, your best option in XNA is to create a Rectangle which will trigger on collision and change the game world.
At the moment I've set up a dual camera scene in Unity. I've created an empty game object for my camera's to inherit from and attached some of the first person controller scripts to this empty game object.
When I run the program in the editor, it runs fine. When I build the project, the game crashes and my camera objects fall through the ground. I've never experienced something like this before in Unity. Attached is a copy of my my current fps set up values.
In the picture, you will see that I have turned gravity off (set it to 0), yet it still falls down when I run the built program.
Has anyone ever come across something like this before? I've spent all day trying to fix this, but I'm getting no where.
I experienced such an issue when my parent object (I usually use capsules to "carry" the camera as the players head like 1.70m above the ground) is set to low ... what happens if you move your camera-guy together with the game object one meter upwards? (so that he falls down a bit against the surface when you start).
Maybe there is a difference in some relations between editor-build and release-build.
Or, in case this isn't the solution, check the spatial positions of your involved objects again. Falling through a terrain is often produced by misplaced reference objects. (I sometimes hung a carrier object under a camera instead of hanging the camera under a carrier object.)
As you say that it works in the editor-build i assume that you have activated collision for the relevant objects.