I am learning Unity and C#. I am trying to use a button to trigger something, but the function is not showing up. Here is the code:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class ExitButton : MonoBehaviour
{
public void OnButtonTrigger () {
Debug.Log("Application Quitting");
Application.Quit();
}
}
And here is a screenshot of where the problem is:
attach that script to a game object as a component then drag that object to OnClick and you will find the functions.
Related
I'm just trying to edit a Button's text on my Unity app whenever the game loads.
ArgumentException: GetComponent requires that the requested component 'Text' derives from MonoBehaviour or Component or is an interface.
Below is my code:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UIElements;
using UnityEngine.UI;
internal class Text
{
internal string text;
}
public class Backend : MonoBheaviour
{
void Start()
{
GameObject.Find("Option 1 Button").GetComponentInChildren<Text>().text = "Sdfsdfsd";
PopulateHeadlines();
}
What am I doing wrong?
Your definition of internal class Text hide the UnityEngine.UI.Text.
Delete your definition and try again.
I feel like this is a poor question to ask, but I couldn't figure out what the issue was.
I was making a ScoreController and I wanted to make it so that a coin object, when hit, would add 100 to the score GUI. All the coins have this script inside of them:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
public class CoinScoreController : MonoBehaviour
{
public void OnTriggerEnter2D(Collider2D collider)
{
transform.parent.parent.parent.Find("Canvas/ScoreUIController").CollectCoin();
}
}
I can recognize that this is a very poor way of getting an object but I can't figure out what the issue is. Here is the script that I was trying to reference:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
public class ScoreController : MonoBehaviour
{
public static int score = 0; //Number of coins
public static GameObject scoreUI; //A gameObject for the CoinUI
// Update is called once per frame
void Start()
{
scoreUI = GameObject.Find("ScoreUI"); //Automatically sets all coins to have their coinUI GameObject equal the CoinUI UI.
}
public void CollectCoin() //Runs when a coin is collected. Makes it disapear and adds one to the counter.
{
gameObject.SetActive(false);
score += 100;
scoreUI.GetComponent<Text>().text = "Score: " + score.ToString();
}
public void KilledMonster()
{
score += 1000;
}
}
The ScoreControllerUI is inside Canvas so I don't think I have the wrong path. Can you not transform.Find() from the scene? Here is my scene:
As I am sure you can tell, I am new to this site and to Unity, so I apologize for any mistakes in the way I asked this question. Feel free to give me constructive criticism on that as well. I am using unity version 2020.3.24f1 and coding in C#. I don't know if this is relevant but I am coding using visual studio.
Sorry again for any dumb mistakes I made.
Instead of using
transform.parent.parent.parent.Find("Canvas/ScoreUIController").CollectCoin();
use
GameObject.Find("Canvas/ScoreUIController").GetComponent<ScoreController>().CollectCoin();
Suggestion: Define a public ScoreController ScoreUIController in your CoinScoreController (I'm assuming your coin is a prefab) then drag and drop the ScoreUIController into it to assign your script to our defined variable.
Now, you would be able to call
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
public class CoinScoreController : MonoBehaviour
{
public ScoreController ScoreUIController;
public void OnTriggerEnter2D(Collider2D collider)
{
ScoreUIController.CollectCoin();
}
}
P.S. : Please follow tutorials and read documentation first, as I can see your code is very process heavy.
I want to take username through InputField in scene 1 and access the username entered by the user in scene 2. I have a InputField and a submit button in scene 1. When click the submit button, the scene changes. How should I go about it?
I tried using DontDestroyOnLoad() but this didn't worked for me.
To change the scene i am using SceneManager.LoadScene(name);
Here I came up with this.I achieved what you wanted with the using PlayerPrefs
I created a InputField and attached a script to it called Save and then I called SaveText() from that script after On End Event in inspector
Save.cs
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
using UnityEngine.SceneManagement;
public class Save : MonoBehaviour
{
InputField userNameField;
void Start()
{
userNameField = GetComponent<InputField>();
}
public void SaveText()
{
PlayerPrefs.SetString("userName", userNameField.text);
Debug.Log(userNameField.text);
}
public void MoveScene(string sceneToLoad)
{
SceneManager.LoadScene(sceneToLoad);
}
}
and in next Scene I attached the script called Load.cs to a text gameobject and in Start() I got information of that String that saved previously
Load.cs
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
public class Load : MonoBehaviour
{
Text userNameText;
void Start()
{
userNameText = GetComponent<Text>();
userNameText.text = PlayerPrefs.GetString("userName");
}
}
Note : I don't know how efficient this is, But it does the work
I was wondering how I could make my button change the screen resolution. I have this so far, but I do not really know how to make it work with the button. Any help is appreicated, thanks!
using UnityEngine;
using System.Collections;
using UnityEngine.UI;
public class Resolution1 : MonoBehaviour {
public GameObject MyButton;
void Resolution11 () {
Screen.SetResolution(1920, 1080, true);
}
}
using UnityEngine;
using System.Collections;
public class NewBehaviourScript : MonoBehaviour {
// Use this for initialization
void Start () {
}
// Update is called once per frame
void Update () {
if (
}
}
Inside the if ( i type Input but it's not exist.
I tried to add in the top using.System.Io; but that's not the solution.
In my unity project i clicked in the menu on Assests > open C# Project and it opened a new mono develop scripting window.
In my unity project i have a firstpersoncharacter and under in it a spotlight.
I want to make in the script a key trigger like if i click on the key F it will turn on the spotlight and if i click on F again it will turn the spotlight off.
if there is no input you should check script:
1.fix compiler errors
2.if you haven't libraries you can not access to input or etc?
using UnityEngine;
using System.Collections;
3.if you can't access to input you should install new monodevelop version from monodevelop.com
using UnityEngine;
using System.Collections;
public class ExampleClass : MonoBehaviour {
public bool mybool;
void Update() {
if (Input.GetKeyDown(KeyCode.F))
GetComponent<Light> ().enabled = !mybool;
}
}
explain:
!false = true
!true = false