I'm quite new to Unity and just started learning UI and made a volume slider in a settings scene. I've learnt how to use those 2 functions to save the PlayerPref of the volume so whenever the player enters the settings scene again, the volume will be saved from last time.
However, I wonder how you can change it so it will do the same but on the main menu? So that way the player doesn't need to go back to settings for it to apply.
The code:
`using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
using UnityEngine.Audio;
using UnityEngine.SceneManagement;
public class SettingsMenu : MonoBehaviour
{
public Slider volumeSlider;
void Start()
{
keepVolumeValue();
GameObject.FindGameObjectWithTag("Music").GetComponent<MusicManager>().playMusic();
}
void Update()
{
if (Input.GetKeyDown(KeyCode.Escape))
SceneManager.LoadScene("MainMenu");
}
private void OnDisable()
{
PlayerPrefs.SetFloat("volume", volumeSlider.value);
}
public void keepVolumeValue()
{
volumeSlider.value = PlayerPrefs.GetFloat("volume", volumeSlider.value);
}
public void setVolume(float volume)
{
mainMixer.SetFloat("volume", Mathf.Log10(volume)*30f);
}`
Ty for any help
Related
- error message
After completing the bullet script operation, I tried to insert the script into Bullet Sprite in 2D Object format, but an error was displayed and the insertion was not possible. I tried turning off and on the editor because I thought it was a bug because I had the same file name and class name, but it didn't work. Do you happen to know about this problem? The editor version is 2021.3.11f1.
Bullet Script
using System;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.Pool;
public class Bullet : MonoBehaviour
{
[SerializeField]
private float speed = 10f;
private IObjectPool<Bullet> pool;
void Update()
{
transform.Translate(Vector2.up);
}
public void SetPool(IObjectPool<Bullet> bulletPool)
{
Launcher.Instance.bulletPool = pool;
}
private void OnBecameInvisible()
{
Launcher.Instance.bulletPool.Release(this);
}
}
I turned off and on the editor and changed the class name because I thought it was different.
I have a setup that includes a joystick + button ( that implements IPointerUpHandler and IPointerDownHandler )
IPointerUpHandler Is not called sometimes when i'm dragging the joystick at the same time ( mobile of course)
Anyone have a solution?
I tried moving the button to a different canvas, did not work.
using System;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.Events;
using UnityEngine.EventSystems;
using UnityEngine.UI;
public class ControllerButton : UIBehaviour, IPointerDownHandler, IPointerUpHandler
{
public UnityAction<bool> onClick;
public RawImage icon;
//Detect current clicks on the GameObject (the one with the script attached)
public void OnPointerDown(PointerEventData pointerEventData)
{
//Output the name of the GameObject that is being clicked
onClick?.Invoke(true);
Debug.Log(name + "Game Object Click in Progress");
}
//Detect if clicks are no longer registering
public void OnPointerUp(PointerEventData pointerEventData)
{
onClick?.Invoke(false);
Debug.Log(name + "No longer being clicked");
}
// Start is called before the first frame update
void Start()
{
}
// Update is called once per frame
void Update()
{
}
}
I'm trying to synchronize text in a unit between players, but I'm getting an error.
I just need to sync the text
[1]: https://i.stack.imgur.com/0l98U.png
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
using Photon.Pun;
using UnityEngine.UIElements;
public class Sync : MonoBehaviourPunCallbacks
{
private PhotonView view;
public Text textGame;
public Text copied;
void Start()
{
view = GetComponent<PhotonView>();
}
public void sync()
{
if(view.IsMine)
view.RPC("ViewAll", RpcTarget.AllBuffered, textGame.text);
}
[PunRPC]
public void ViewAll(string tG)
{
tG = copied.text;
Debug.Log(tG);
}
}```
пппппппппппппппппппппппппппппппппппппппппппппппппппппппппппппппппппппппппппппппппппппппппппппппппппппппппппппппппппппппппппппппппппппппппппппппппппппппппппппппппппппппппппппппппппппппппппппппппппппппппппппппппппппппппппппппппппппппппппппппппппппппппппппппппппппппппппппппппппп
The error is telling you that the "view" variable has not been assigned and is therefore null.
To assign it you can make that field public, and in the inspector you
assign the photonView you want, I guess that of the player.
Alternatively you can assign "view" not from the inspector but in a
Start() or Awake() method. If the photonView you want to associate is
on the same GameObject as the script, you can write like this:
void Awake()
{
view = GetComponent();
}
However, the easiest answer is to replace "view" with "photonView".
It will have the same result as the solutions described above, but it will be very simple.
enter code here
public void sync()
{
if(photonView.IsMine)
view.RPC("ViewAll", RpcTarget.AllBuffered, textGame.text);
}
I have not directly given you the simple slogan to make you fully understand the problem and to help you solve it.
I am trying to make a FlappyBird on WebGL with Unity. I made scores system, and there is no bug finded while runing on unity editor, but when I build with WebGL, Text don't shows up. Only legacy Text (Not TextMeshPro) causes this problm, so it would be helpful too if there is a way to use TextMeshPro. https://r0k0r.github.io/FlappyBirdWebGL is my game link, and https://github.com/R0K0R/FlappyBirdWebGL is my github link. I don't know this will help, but I am currently coding with ubuntu.
this is my Score.cs code that returns current score:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
public class Score : MonoBehaviour
{
public static int score = 0;
// Start is called before the first frame update
void Start()
{
}
// Update is called once per frame
void Update()
{
}
void OnTriggerEnter2D(){
score++;
}
public static int returnScore(){return score;}
}
and this is my ApplyScore.cs code that applys score to text gameobject.
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
public class ApplyScore : MonoBehaviour
{
public Text ScoreText;
// Start is called before the first frame update
void Start()
{
ScoreText.text = "0";
}
// Update is called once per frame
void Update()
{
ScoreText.text = Score.returnScore().ToString();
}
}
this is what it looks like
and this is what it should look like
UI components and GameManager
I'm following a video tutorial on Unity and the person just dragged those items in the GameManager slots, what I am not able to do.
Code :
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
public class GameManager : MonoBehaviour
{
public GameObject ballPrefab;
public GameObject PlayerPrefab;
public Text scoreText;
public Text ballsText;
public Text levelText;
void Start()
{
}
void Update()
{
}
}
The problem is that you selected the GameManager inside your project folder and not inside your hierachy. Thats why you can drag & drop Prefabs inside of it but not your Text inside your hierachy.