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.
Related
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
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.
I'm working on a death script for my game in Unity. I made a 3d Box without textures underneath my level and made its Collider isTrigger = true. I now added a script to the box that reloads the current scene when the player enters the trigger. Its 2 lines of code and I don't know why but I get the error:
Assets\scripts\death.cs(20,32): error CS0103: The name 'currentScene' does not exist in the current context
The Code:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.SceneManagement;
public class death : MonoBehaviour
{
void Start()
{
Scene currentScene = SceneManager.GetActiveScene();
}
private void OnTriggerEnter(Collider other)
{
SceneManager.LoadScene(currentScene.buildIndex);
}
}
I know that the comments above already noticed your problem was the local variable and thanks to them, but this is just about to optimize your code and memory. you can just keep OnTriggerEnter only and delete Start.
private void OnTriggerEnter(Collider other)
{
SceneManager.LoadScene(SceneManager.GetActiveScene().buildIndex);
}
there's no need to store the scene in a variable if we will not use in a further needs. it will be just a waste of memory (bad habit)
I am using the FireComplex particle to generate an explosion (a bomb) when the user presses spacebar. The fire is playing on awake which I don't want and it doesn't seem to include an option to disable it.
Can this be coded to only activate the fire particle when I press space? I have tried the following:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class CombatController : MonoBehaviour {
//public GameObject enemy;
[SerializeField]
public GameObject Bomb;
public GameObject Fire;
if(Input.GetKeyUp(KeyCode.Space)) {
BombExplosion ();
}
public void BombExplosion () {
//Create the Bombs Explosion Particle Effect.
Instantiate (Fire, this.gameObject.transform.position, Quaternion.identity);
Fire.Emit(5); //Burn for 5 seconds
}
Unity seems to be ignoring the Fire.Emit function.
The reason, I think, why the Bomb goes off after executing the game is because of the onviously incorrect logic in GetKeyUp. I doubt that it what you need.
Change it to GetKey instead.
Hope that clarifies the issue.
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