Unity C# gameobject.setactive(false) in first play - c#

How do I set gameobject(myCanvas) to be false during the first play?
I've put this script into the cube, and when I click the cube it will show the Canvas.
I haven't been clicking on the cube but, the canvas has already come out.
using UnityEngine;
using UnityEngine.UI;
using System.Collections;
public class MouseDownText : MonoBehaviour {
public Canvas myCanvas;
void Start()
{
// first start game. gameobject will turn off
myCanvas.gameObject.SetActive(false);
}
void OnMouseDown()
{
// for switch on/off
myCanvas.gameObject.SetActive(!myCanvas.gameObject.activeSelf);
}
}

From what I understand you have a canvas component attached to a game object (a cube) and you want to use mouse clicks to toggle whether or not the canvas is active.
Have you tried:
using UnityEngine;
using UnityEngine.UI;
using System.Collections;
public class MouseDownText : MonoBehaviour {
public Canvas myCanvas;
// Use this for initialization
void Start () {
myCanvas = GetComponent<Canvas> ();
myCanvas.enabled = false;
}
// Update is called once per frame
void Update () {
}
void OnMouseDown()
{
// for switch on/off
if (myCanvas.enabled)
myCanvas.enabled = false;
else
myCanvas.enabled = true;
}
}
Also make sure you haven't accidentally enabled it in some other code somewhere.

Related

The flashlight disappears, but doesn't appear back

I wanted to add a flashlight to my game. When I press the button to make it disappear, it disappears, but when I press the button to try to make it appear again it doesn't. I've been trying to find the solution for the past hour and a half and looked into the docs too but I didn't find anything.
using System.Collections.Generic;
using UnityEngine;
public class flashlight : MonoBehaviour
{
// Start is called before the first frame update
void Start()
{
}
// Update is called once per frame
void Update()
{
if (Input.GetKeyDown(KeyCode.Alpha1))
{
gameObject.SetActive(true);
}
if (Input.GetKeyUp(KeyCode.Alpha2))
{
gameObject.SetActive(false);
}
}
}
The script is being deactivated along with the gameObject so it isn't listening for Input.GetKeyUp(KeyCode.Alpha2).
To get round this create an empty GameObject to hold the script and make the light a child of said GameObject, then when you deactivate the light the script is still active and listening for the Input.
Update the script like this to assign the light child
public class flashlight : MonoBehaviour
{
public GameObject light;//Assign this is the inspector
void Update()
{
if (Input.GetKeyDown(KeyCode.Alpha1))
{
light.SetActive(true);
}
if (Input.GetKeyUp(KeyCode.Alpha2))
{
light.SetActive(false);
}
}
}

How do you hide a UI object (Video Player) after time (Unity)

I am very new to using Unity and I am surviving off tutorials from YouTube. As soon as my game is launched, a video is started using Video Player. I am hoping to make the video hidden after the video has finished playing to reveal my menu screen. I do have a script that I have used to hide the video player
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
public class HideVideo : MonoBehaviour
{
public GameObject VideoPlayer;
public void HideVideoPlayer()
{
VideoPlayer.gameObject.SetActive(false);
}
}
The problem is, the closest I have gotten to actually hiding my video is by setting it to an onclick event via a button. How do I make the video player hide after the video is finished playing? Thanks.
Why not simply hide it, when it stops playing and put it in Update?
void Update() {
if (!(VideoPlayer.isPlaying)) {
VideoPlayer.gameObject.SetActive(false);
}
}
A complete script could be look like this:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
public class HideVideo : MonoBehaviour
{
public GameObject VideoPlayer;
public bool isPlayerStarted = false;
void Update() {
if (isPlayerStarted == false && VideoPlayer.IsPlaying == true) {
// When the player is started, set this information
isPlayerStarted = true;
}
if (isPlayerStarted == true && VideoPlayer.isPlaying == false ) {
// Wehen the player stopped playing, hide it
VideoPlayer.gameObject.SetActive(false);
}
}
}

Unity SetActive() doesn't activate object

I have a problem that I can't solve by my self.
I have a Pause Button which need to activate pause panel on scene, but nothing work.
1. I have a public GO "Panel" attached in the inspector.
2.Inspector writes that: "There is no 'GameObject' attached to the "Panel" game object, but a script is trying to access it."
3.Script on always active GO.
4.At start Panel is Active.
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
using UnityEngine.SceneManagement;
public class ButtonController : MonoBehaviour {
private Scene ActiveScene;
private GameController gm;
public GameObject panel;
// Use this for initialization
void Start ()
{
gm = GetComponent<GameController>();
ActiveScene = SceneManager.GetActiveScene();
panel.SetActive(false);
}
public void Pause()
{
Debug.Log("Pause");
panel.SetActive(true);
Time.timeScale = 0;
}
public void Menu()
{
SceneManager.LoadScene(0);
}
public void Restart()
{
SceneManager.LoadScene(ActiveScene.buildIndex);
}
public void Play()
{
Time.timeScale = 1;
panel.SetActive(false);
}
Glad if u can help!
I solved the problem: I attached the script twice.

Calling dialogue script from NPC using Interactable script (Brackeys' system)

I have followed a tutorial on Youtube (https://www.youtube.com/watch?v=_nRzoTzeyxU) on how to create a dialogue system for a game. Since my game is a platformer/RPG, I am currently attempting to adapt this system to where the player can walk up to an NPC and press the "Submit" button to access their dialogue, instead of clicking a button on the canvas/UI.
So far I have created an Interactable script that allows the player to detect if they are in the range of the invisible sphere collider that is equipped to the NPC, which is working. If I try to access the dialogueTrigger script that is equipped to the NPC however, I get a NullReferenceException error. I would like some help on how to properly call the dialogueTrigger script from the NPC and trigger the dialogue event, as I am very new to code and I only have this so far. Any help would be appreciated.
EDIT:
NullReferenceException: Object reference not set to an instance of an object
Interactable.Update () (at Assets/Scripts/Interactable.cs:34)
Dialogue Trigger is:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class DialogueTrigger : MonoBehaviour
{
public Dialogue dialogue;
public void TriggerDialogue ()
{
FindObjectOfType<DialogueManager>().StartDialogue(dialogue);
}
}
Interactable is:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class Interactable : MonoBehaviour
{
private GameObject triggeringNpc;
private bool triggering;
public DialogueTrigger Diag;
void Start()
{
}
void Update()
{
if(triggering)
{
Debug.Log("Within Range");
if (Input.GetButtonDown("Submit"))
{
Debug.Log("Pressed the Interact Button");
Diag.TriggerDialogue();
}
}
}
void OnTriggerEnter(Collider other)
{
if(other.tag == "NPC")
{
triggering = true;
triggeringNpc = other.gameObject;
}
}
void OnTriggerExit(Collider other)
{
if(other.tag == "NPC")
{
triggering = false;
triggeringNpc = null;
}
}
}
The culprit would be your Diag reference. Have you correctly dragged a DialogueTrigger prefab from your Hierarchy window and into the public field on Interactable?

Unity3D C# Inheritance Issue

I have two c# scripts.
One for the cannon:
using UnityEngine;
using System.Collections;
public class CannonScript : MonoBehaviour {
public GameObject bullet; //bullet object
public Transform spawnpoint; //spawn point
float bulletspeed=-3000f; //bullet speed
public int bullets=10; //number of bullets
void Update () {
if ((Input.GetKeyDown ("space") || Input.GetMouseButtonDown(0)) && bullets > 0) {
GameObject newbullet; //make a new bullet
newbullet=Instantiate(bullet,spawnpoint.position, spawnpoint.rotation)as GameObject; //instantiate the new bullet
newbullet.rigidbody.AddForce(spawnpoint.forward * bulletspeed); //add force to the new bullet
bullets--; //decrease the number of bullets left
}
}
}
Another for the UI text on the screen:
using UnityEngine;
using UnityEngine.UI;
using System.Collections;
public class BulletCountScript : CannonScript { //inherit from the CannonScript
Text text;
void Start () {
text = GetComponent<Text> ();
}
void Update () {
text.text = bullets.ToString(); //display the number of bullets
}
}
When I run it, the UI text on the screen never changes.
I know the CannonScript is working properly, because I can only shoot 10 bullets.
How can I make my UI text display the amount of bullets left?
Am I doing something wrong with inheritance?
I think you are missunderstanding the inheritance concept. The problem I see is both classes are different instances, so that is the reason because the text is always the same.
To achieve what you want, just do something like this:
using UnityEngine;
using UnityEngine.UI;
using System.Collections;
public class BulletCountScript : MonoBehaviour {
public CannonScript cannon;
Text text;
void Start () {
text = GetComponent<Text> ();
}
void Update () {
text.text = cannon.bullets.ToString(); //display the number of bullets
}
}
Just drag the object who owns the CannonScript to the game object attribute
at the BulletCountScript, now, both of them are referencing the same instance.
I hope this helps! Good luck!

Categories

Resources