enable/disable Objects in Unity - c#

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

Related

FMOD parameter not changing through C# script

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!

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.

Empty GameObject not deactivating

everyone.
Basically, I have a prefab with a canvas and an empty GameObject. This GameObject, PlayAd, contains its own canvas and a button to play ads and let the users skip a level. PlayAd's button is covering the other canvas's continue button, which is only supposed to be accessible if the user passes a level. All my script is supposed to do is deactivate PlayAd on trigger enter so the users can get to the continue button. However, it never goes away after I reach the trigger to cause this. Any help on this would be appreciated. Thank you.
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
public class NewLevScript : MonoBehaviour
{
public GameObject PlayAd;
// Start is called before the first frame update
private void Start()
{
PlayAd = GameObject.Find("PlayAd");
}
private void OnTriggerEnter(Collider other)
{
if (other.tag == "Platyer")
{
PlayAd.SetActive(false);
}
}
}
Boy, do I feel dumb... I had to remove the "t". It works now.

GameObject prefab is not showing in scene when activated in my script

I have 2 classes attached to 2 different GUIs, PlayerInfoGUI and DiplomacyGUI.
I am trying to have a method in PlayerInfoGUI create a list of buttons dynamically which, when clicked will then pull up DiplomacyGUI. (Both GUIs as well as the dynamic buttons have their own prefabs created)
My PlayerInfoGUI class dynamically populates a panel with buttons using the PopulatePlayerList method.
using UnityEngine;
using UnityEngine.UI;
using UnityEngine.EventSystems;
using System.Collections;
using System.Collections.Generic;
public class PlayerInfoGUI : MonoBehaviour
{
public Image[] guiElements;
public GameObject playerInfoButtonPrefab, canvasParent;
public GameObject diplomacyMenu;
void Awake ()
{
PopulatePlayerList (GameEngine.competingPlayers);
}
void PopulatePlayerList (List<CompetingPlayer> players)
{
for (int i = 0; i < players.Count; i++) {
GameObject go = (GameObject)Instantiate
(playerInfoButtonPrefab);
Button playerInfoButton = go.GetComponent<UnityEngine.UI.Button>
();
CompetingPlayer receivingPlayer = players [i];
playerInfoButton.onClick.AddListener (() => handleDiplomacyMenu
(receivingPlayer));
go.transform.SetParent (canvasParent.transform, false);
}
}
public void handleDiplomacyMenu (CompetingPlayer receivingPlayer)
{
diplomacyMenu.SetActive (true);
}
}
The listener on the PlayerInfo Button is firing when clicked, but the diplomacyMenu GameObject is not showing up in the scene. Most of the research I have read says this should be a simple diplomacyMenu.SetActive(true), or a diplomacy.gameObject.SetActive(true), but this doesn't work.
I have confirmed that the code is being run, but the object cannot be seen.
Thank you in Advance!
PlayerInfoPrefab
PlayerInfoButtonPrefab
DiplomacyPrefab
You're using diplomacyMenu.gameObject when diplomacyMenu is already a game object, this might cause issues. Also make sure the prefab that is represented by diplomacyMenu has its children enabled. I think SetActive() just sets the top level parent to enabled, not any nested objects.

Unity3d C# - How to check if a button has been pressed, and then instantiate an object

So for the past two days (no joke) I have been trying to figure out how to check if a button is pressed, and then if so, make it instantiate an object. I have tried multiple methods so far and maybe one of those methods were closer to getting where I wanted but for now I will just show what I have currently. Right now, the problem is me detecting if the button has even been clicked in the first place.
using UnityEngine;
using System.Collections;
using UnityEngine.UI;
using UnityEngine.EventSystems;
public class Master : MonoBehaviour
{
public Button storeButton;
public Transform hands;
public GameObject gun1;
void OnEnable()
{
gun1 = GameObject.FindGameObjectWithTag("Weapon1");
hands = GameObject.FindGameObjectWithTag("Player").transform;
// storeButton.onClick.AddListener(MyFunction);//adds a listener for when you click the button
storeButton.onClick.AddListener(() => MyFunction());
}
void MyFunction()
{
Debug.Log("YOUR PRESSED THE DAMN BUTTON");
// Instantiate(gun1, hands.position, hands.rotation);
GameObject.Instantiate(gun1, hands.position, hands.rotation);
}
}
Add an EventSystem to your Scene.
Otherwise you cant click Buttons an other Stuff.
http://docs.unity3d.com/Manual/EventSystem.html

Categories