FMOD parameter not changing through C# script - c#

I'm currently trying to develop my first mini game (a VR musical experience).
I'm trying to have the value of a slider control an FMOD parameter, but nothing happens. Plus the function does not show in the On Value Changed of the slider...
I'm already using that slider to control the transparency of a material and if I try changing the FMOD parameter manually in the inspector it works just fine.
Here is the script:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
public class FMODParameters : MonoBehaviour
{
FMODUnity.StudioEventEmitter changeVolume;
FMOD.Studio.EventInstance bass;
public string bassVolumeParameter;
public Slider slider;
void Start()
{
changeVolume = GetComponent<FMODUnity.StudioEventEmitter>();
bass = FMODUnity.RuntimeManager.CreateInstance("event:/Beat/2D/2D Bass");
slider = GetComponent<Slider>();
}
// Update is called once per frame
void Update()
{
SetBassVolume();
}
void SetBassVolume()
{
bass.setParameterByName(bassVolumeParameter, slider.value / 250);
}
}
Thanks in advance for the help! :)

The problem is that you have to add the function in the onValueChanged () listener.
To do it:
Select the gameObject with the Slider component;
Click the "+" button in the OnValueChanged () listener below;
In the empty field, under "Runtime only", drag the gameObject with
this script;
Click the box that now has the name "No Function";
In the component list, place the cursor over the script name, and
under the "Dynamic parameters" category, the SetBassVolume () method.
But first you should tweak the method a bit, like so:
void SetBassVolume(float _value)
{
bass.setParameterByName(bassVolumeParameter, _value / 250);
}
Good work!

Related

enable/disable Objects in Unity

I am totally new in programmation and unity, so I have hard time with basically everything!
Here is my issue : I have a 2D static game with a grid of boxes. each box is made of buttons to click.
I want all the boxes but one not visible at the beginning, and then the box have a button to make boxes appears one by one.
here is my code :
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class OpenBox : MonoBehaviour
{
// Start is called before the first frame update
private GameObject boite1;
void Start()
{
box1 = GetComponent<Box1> ();
}
void Update()
{
if (Input.GetKeyUp(KeyCode.Space))
{
box1.enabled = true;
}
}
}
The "Box1" is underline in red with message : CS0246, The type or namespace name could not be found.
I am not sure I know how to refer to the game object.
thank you for your help !
try use
gameObject.SetActive(false);
more detail unity doc

Unity changing text with script c#

I'm trying to add a scoring method in my Unity phone game by changing the UI text.
Here is My code:
using UnityEngine;
using UnityEngine.UI;
public class ScoreDisplay : MonoBehaviour
{
// Start is called before the first frame update
[SerializeField]private Text m_MyText;
void Start()
{
m_MyText.text = "This is my text";
}
// Update is called once per frame
void Update()
{
m_MyText.text = "UWU";
}
}
Nothing pops up when I search for a text object:
And I cannot drag the mesh text into the public variable. Any help would be appreciated, thank you.
Text is a legacy element and is mostly unused. Instead, you want Text Mesh Pro's text component which is under the TMPro namespace. The componant is called TextMeshProUGUI.
Replace
[SerializeField]private Text m_MyText;
with
[SerializeField]private TMPro.TextMeshProUGUI m_MyText;
The TextMeshProUGUI componant has a text field, so your code should work as normal after replacing the line.

Hover over one button affects other button as well

I have two buttons like so:
I am using DOTween so that when I hover over the button it highlights by enlarging slightly and tweens into it to look smooth. However, I have two separate scripts attached for each button which do exactly the same thing but when I hover over one of the buttons the other button also enlarges. I've tried using the buttons under the same canvas which didn't work. So then I tried using two canvases for separate buttons and it still the same issue.
Looks like this:
https://vimeo.com/user105553995/review/517200220/41fbe8d619
My code is in different scripts for each button but exactly the same:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.EventSystems;
using DG.Tweening;
public class OtherButton : MonoBehaviour
{
// Start is called before the first frame update
private Vector3 defaultScale;
// Start is called before the first frame update
void Start()
{
defaultScale = this.transform.localScale;
}
// Update is called once per frame
void Update()
{
WhilePointerHover();
}
private void WhilePointerHover()
{
if (EventSystem.current.IsPointerOverGameObject())
{
Vector3 enlarge = defaultScale*1.2f;
this.transform.DOScale(enlarge, 0.05f);
Debug.Log(this.gameObject.name);
}
else
this.transform.localScale = defaultScale;
}
}
Any help is appreciated!
Code checks if you are over any UI object, so any script will react.
You should use https://docs.unity3d.com/2019.1/Documentation/ScriptReference/UI.Selectable.OnPointerEnter.html
And
https://docs.unity3d.com/2019.1/Documentation/ScriptReference/UI.Selectable.OnPointerExit.html
So it will only react to the object to which it is attached to. Note that the Image or Button component needs to be on same object.

No dynamic float visible for attachment in unity

I am trying attach a particle system's gravity modifier with a slider but it is not visible. Here is a screen shot of what I see.
Should I give this up and try to attach the value using a script instead ? ? ?
You cannot see it from the Editor because main.gravityModifier is a type of MinMaxCurve not float so you cannot assign the Slider value which is float to it from the Editor. There is a function that let's you do that from scripting. That's why it works from script.
Do that from code. It looks like you already got an answer but there are so many things to improve.
1.Don't use GameObject.Find every frame.
You are currently doing this 2 times in a frame with GameObject.Find("Slider") and GameObject.Find("Particle System"). Cache the Slider and the ParticleSystem components. Move those functions to the Start or Awake function.
2.Do not use the Slider value to modify main.gravityModifier every
frame.
Use the onValueChanged event to subscribe to the Slider so that you will only modify main.gravityModifier when the slider value changes. The Update function is no longer needed when you do this.
private ParticleSystem ps;
public Slider slider;
// Use this for initialization
void Start()
{
//Cache the Slider and the ParticleSystem variables
slider = GameObject.Find("Slider").GetComponent<Slider>();
ps = GameObject.Find("Particle System").GetComponent<ParticleSystem>();
}
void OnEnable()
{
//Subscribe to the Slider Click event
slider.onValueChanged.AddListener(delegate { sliderCallBack(slider.value); });
}
//Will be called when Slider changes
void sliderCallBack(float value)
{
Debug.Log("Slider Changed: " + value);
var main = ps.main;
main.gravityModifier = value;
}
void OnDisable()
{
//Un-Subscribe To Slider Event
slider.onValueChanged.RemoveAllListeners();
}
I got the scripting answer, but if any of you has a better solution you are welcome to share it.
using UnityEngine;
using UnityEngine.UI;
public class SliderScript : MonoBehaviour {
private double sf;
private ParticleSystem ps;
// Use this for initialization
void Start () {
}
// Update is called once per frame
void Update () {
sf = GameObject.Find("Slider").GetComponent<Slider>().value;
ps = GameObject.Find("Particle System").GetComponent<ParticleSystem>();
var main = ps.main;
main.gravityModifier = (float)sf;
print(sf );
}
}

Find a GameObject and check if the user has clicked on it

I have recently been making the UI for a game I am creating and have run into a problem.
I have been trying to find a way in the new Unity 4.6 for the user to be able to click on a player card and have it select the player they clicked on.
public void Panel1Click()
{
GameManager.Player1Select ();
}
This is the way I am doing it at the moment, calling this when the player clicks on Panel 1, there are also 3 more for each of them.
I have been researching different methods on how to find the object the player clicks the execute the correct selecting code.
if (GameObject.Find ("Panel 1"))
{
print ("Click Panel 1");
GameManager.Player1Select();
}
This is one of the methods I tried, however nothing gets called. (Because it just checks if the object exists/is true? I think).
All these methods are linked to the EventSystem component on the panels.
Is there a more efficient way of condensing all the functions and just checking which panel the player clicks on?
You can add a collider to the game object (sprite), and then in the OnMouseDown function to test if it is clicked.
Select the card --> Add a box collider to it --> Add a MonoBehaviour script and attach to the card --> In the script, add function:
bool bClicked = false;
void OnMouseDown()
{
Debug.Log(gameObject.name + " is clicked");
bClicked = true;
}
You can actually have one parameter for your click handler. Supported types are: int, float, string and object reference. So you can define your handler like this:
public void SelectCharacter(int character) {
GameManager.PlayerSelect(character)
}
Then just set the parameter in the event trigger.
Create a script and put it on the object(Panel) you want to interact with, then try this code. In this example it finds the parent panel and sets it to inactive
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.EventSystems;
public class CloseInventory : MonoBehaviour, IPointerDownHandler
{
GameObject inventoryPanel;
// Use this for initialization
void Start()
{
inventoryPanel = GameObject.Find("Inventory Panel");
}
public void OnPointerDown(PointerEventData eventData)
{
//SET WHAT TO DO HERE
inventoryPanel.SetActive(false);
}
}

Categories