C#/UNITY How do I make a button change the screen resolution? - c#

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);
}
}

Related

Changing text of a Button in Unity upon loading

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.

Tranform.Find Isn't Getting the Correct GameObject

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.

Function not showing up on unity button

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.

What is the right way to use get key down in this scenario?

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using System;
public class Q : MonoBehaviour
{
void OnTriggerStay2D(Collider2D collision)
{
if (Input.GetKeyDown("q"))
{
Destroy(collision.gameObject);
}
}
}
ok so i wrote code that is supposed to detect when another object is colliding with it to allow the player to press a key to destroy the collider. the code is able to detect a collider but is not able to detect a key press while the collider is being detected. I have zero clue to as why this is happening so if anybody can help that would be great thank you
If you look at the documentation for GetKeyDown, you'll see that it needs to be in your Update callback. This would be super easy to fix though!
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using System;
public class Q : MonoBehaviour
{
bool _qPressed;
void Update()
{
_qPressed = Input.GetkeyDown("q");
}
void OnTriggerStay2D(Collider2D collision)
{
if (_qPressed)
{
Destroy(collision.gameObject);
}
}
}

How to pass data from one scene to another?

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

Categories