I'm creating a volume slider in my settings menu and have this PlayerPref set up to hold it:
public void VolumeApply()
{
PlayerPrefs.SetFloat("masterVolume", AudioListener.volume);
StartCoroutine(ConfirmationBox());
}
However it's not actually saving anything to the PlayerPref when testing it in my UI.
I checked my PlayerPrefs editor and it's not showing as a float despite me explicitly setting the masterVolume PlayerPref as a float??
I've tied deleting all PlayerPrefs which hasn't fixed it.
https://imgur.com/a/Zo5NrQi
Any ideas?
Thanks in advance.
Related
Today my tutor was helping and explaining to me how to add animations into my game, we added wobble effects to my 4 buttons. (pink, blue, yellow, purple) We fully compelted the first button together and then he left me to do the other 3 using what he had taught me. I thought I had done it correctly beause after adding the need code to all 4 of the buttons the pink button's wobble animation works, but the other 3 don't do the animation.
I was looking through to see if something was different and in the Animation dock they come up yellow and say (Missing!) even the pink button that works has this come up. I don't know what this means or how to fix it and would appreciate if anyone could help and explain this to me! Thankyou.
{
if (Input.GetButtonDown("Horizontal"))
{
if (Input.GetKeyDown(KeyCode.LeftArrow))
{
//play pink wobble animation
m_Animator.SetTrigger("Pinkhit");
}
if (Input.GetKeyDown(KeyCode.RightArrow))
{
//play purple wobble animation
m_Animator.SetTrigger("PurpleHit");
}
}
if (Input.GetButtonDown("Vertical"))
{
if (Input.GetKeyDown(KeyCode.DownArrow))
{
//play blue wobble animation
m_Animator.SetTrigger("BlueHit");
}
if (Input.GetKeyDown(KeyCode.UpArrow))
{
//play yellow wobble animation
m_Animator.SetTrigger("YellowHit");
}
}
}
The property key frames in AnimationClips are string based on the property paths which include the type names of the component and the field names they belong to.
The (!Missing) can mean e.g. the following
you removed the according MonoBehaviour from your object
you renamed your according MonoBehaviour class
you renamed the animated fields
this AnimationClip was originally for another object and now you are trying to use it for an object which doesn't have according MonoBehaviour / component attached. (which basically equals the first case)
You renamed or changed the hierarchy structure of the according child GameObject which is nested under the one with the Animator attached. (Renaming the root object would not be an issue)
therefore the according property paths that should be animated by your AnimationClip can not be found on this object.
Since the Scale belongs to Transform which is a component each and every GameObject has to have none of the upper points can be the case here and you most probably happened to rename the object like so:
This is of course a little bummer especially when working with prefabs, bu for the AnimationClips to work properly
either make sure to put the Animator directly on the object that is supposed to be animated (in that case naming is no issue)
or make sure you don't rename or change the hierarchy structure of according child objects
I'm using Unity 2020.3.3f1 (HDRP)
I have a prefab (cube) which has a emissive material on it.
After pressing the mouse button, I want it to increase its Emission intensity by 10.
Problem that I encountered:
The Inspector shows me that the Intensity is infact changing but the Game does not represent these changes (this means it's not getting "brighter" even though the material property says it does).
Now when I increase the amount via the Inspector manually, just by 0.1 even, all of the sudden the changes are now visible.
I think I tried everything now without luck...
How the code looks like in a nutshell:
public Material cubeMaterial;
private float intensity = 10;
if("mouseClick"){
intensity += 100;
cubeMaterial.setFloat("_EmissiveIntensity", intensity);
}
I suppose you are using the default HDRP/Lit shader for your material. If that's so, you can access your cube emission intensity via "_EmissiveColor" shader keyword like this:
cubeMaterial.GetColor("_EmissiveColor");
which returns a Color value.
And you can modify it in a similar way:
cubeMaterial.SetColor("_EmissiveColor", startingEmission * 1.1f);
In general, when working with HDRP material shaders it's always safe to look up the shader keywords, you can do it via navigating to your material in the inspector, clicking the kogwheel and selecting edit shader, which opens the .shader file.
If you intend to modify only this gameObject's material, I recommand you to use MaterialPropertyBlock to modify a property of your material.
If you don't, a new material will be created behind the scene and can lead to memory problems.
To do this, get a reference to the gameObject's renderer, get its property block, modify it and reassign the modified property block.
You can learn more on this documentation
Hope that helped ;)
How can I reset a prefab in Unity to it's initial state when I click a button?
Absolute beginner, not sure where to start. Any help would be appreciated. Thanks!
As TEEQNE said, you'll have to cache (save) the values when you start the game (on the void Start(){} method probably), then whenever you press the button (on the button's.onClick method) you'll want to reset those values.
Another strategy which could save you from the potentially tedious work of caching each value you want reset, would be to simply destroy the gameobject and reinstantiate it using the prefab.
void OnReset(){
var resetGameobject = Gameobject.Instantiate(myPrefab);
Destroy(this);
}
If you're resetting a lot then you might run into some performance issues with the second approach, but it probably won't be a concern in your case.
Let me know if you need a more detailed walkthrough/code examples.
Probably the simplest way would be to destroy this object and instantiate a new one from this prefab.
You could create some sort of ButtonManager script which holds the reference to the currently active object and when the button is clicked, it destroys it and creates a new one in its place, like this:
class ButtonManager: MonoBehaviour
{
// you can drag the prefab in the inspector to this field
public GameObject prefab;
GameObject actObject;
// you set this method to be launched in the button's menu
void OnClick()
{
Destroy(actObject);
actObject = Instantiate(prefab);
}
}
Another way (better optimized and preferable in production and bigger games) would be to just write some sort of Reset function that reverts all the changes made to this object.
I am wondering is there any way to let gravity affect items in editor mode? Normally I set the rigidbody to a object and hit play button, then I can see the object will be falling according to gravity. But How can I do this without hitting play button?
I googled a lot and I can't find any document showing me how to do it.
Thanks
This answer by ThePilgrim on the Unity Q&A site seems to answer this question:
You can simulate physics in the editor by setting
Physics.autoSimulation to false, and using Physics.Simulate() to
advance physics frame by frame until your objects are settled.
Here is an example editor window:
using UnityEditor;
using UnityEngine;
public class ScenePhysicsTool : EditorWindow {
private void OnGUI()
{
if (GUILayout.Button("Run Physics"))
{
StepPhysics();
}
}
private void StepPhysics()
{
Physics.autoSimulation = false;
Physics.Simulate(Time.fixedDeltaTime);
Physics.autoSimulation = true;
}
[MenuItem("Tools/Scene Physics")]
private static void OpenWindow()
{
GetWindow<ScenePhysicsTool>(false, "Physics", true);
}
}
I use this asset called 'Editor Physics Simulator' from the Unity asset store.
Link to asset: https://assetstore.unity.com/packages/tools/level-design/editor-physics-simulator-221538
It only simulates non-kinematic Rigidbodys.
You can simulate all selected Rigidbodys, or ones that are the child of a selected GameObject, or all Rigidbodies at once.
You can simulate for a set amount of time (with interrupts) or manually as long as you want, and you can even still undo/redo the simulation with the default Unity editor keybinds.
It also lets you record animation clips of the physics simulations.
is there anyway to draw stuff on scene view without calling OnSceneGUI?
or is there a way to get OnSceneGUI to get called even if the object with the script attached is not selected?
Edit: Looks like I wasn't explicit enough on what I was asking for so here's a little update:
I have a whole bunch of controls shown as GUI objects on my Game Scene that are used by the game designers to more easily test the game. Since these tools are for development use rather than deployment, I wanted to move these to the scene window instead.
Unfortunately I am only able to display these GUI tools if the object that contains the script with the "OnSceneGUI" method is selected. Instead I want them to be displayed whenever the game is running and regardless of the selected object.
I have found the solution to my issue.
If you want your GUI stuff to always be displayed on the scene window you can do the following:
public class MyClass{
static MyStaticConstructor() {
SceneView.onSceneGUIDelegate += OnScene;
}
static void OnScene(SceneView sceneView) {
// Draw GUI stuff here for Scene window display
}
}
This code will run as soon as you press play and will draw whatever you wish on your scene window.
Hope it will help others!
There are many ways to use GUI,
The easiest but least efficient way is using the GUI class on OnGUI()
GUI.Label(new Rect(10,10,10,10), "This is a label");
GUI.Label(new Rect(10,10,10,10), "Your Texture2D here");
Any active monobehaviour will run OnGUI() if its defined. So it can be attached to any gameObject. You can create an empty gameObject in the scene and call it "GuiGameObject" for example and attach the script there. That way it wont be mixed in with your gameplay script.
There are also GUI textures --> More Info on GUITexture
Also I recommend checking out nGUI
Edit:
For OnSceneGUI You can try Editor.Repaint, you can use it to make sure that the inspector updates changes made inside of OnSceneGUI