How to pass data from one scene to another? - c#

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

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.

Unity : Can't drag GameObject with Text on Script reference

I'm trying to change the Text of an UI GameObject with a script but Unity doesn't let me drag the GameObject with the text on the script reference and when i do public GameObject it says GameObject is not a Text.
using System.Collections;
using System.Collections.Generic;
using System.Globalization;
using System.Security.Cryptography.X509Certificates;
using UnityEngine;
using UnityEngine.UI;
public class GhostName : MonoBehaviour
{
public Text NameText;
void Start()
{
NameText.text = "The Name is : ";
}
}
You need to store NameText as a reference to a GameObject, and then call GetComponent<Text>() to get the Text component on the object. This way, you'll be able to drag the game object into the 'NameText' field in the Unity Inspector window.
...
public class GhostName : MonoBehaviour
{
public GameObject NameText;
void Start()
{
NameText.GetComponent<Text>().text = "The Name is : ";
}
}
You should also check that GetComponent<Text>() is not equal to null before setting the value of .text.
If anyone still get this issue, I had to change from "TextMeshPro" to "Text" Object.
Disclaimer: I am totally new to Unity. But it seems I have the same issue.
Under Hierarchy -> right click -> UI, there is only "Text - TextMeshPro" option in my Unity version (2021.3.4f1). I don't know why "Text" is missing. (anyone concur with me?)
So I fail to follow this step in a beginner tutorial, I anyway just select TextMeshPro instead. Then I just cannot drag to script. I guess it is because the tutorial is using Text type not TextMeshPro and they are not compatible.
Later I realize that, to enable the drag action, one cannot use [SerializeField] Text xxx; but must use [SerializeField] TextMeshProUGUI xxx;.
Pls also include using TMPro; in the header.
I used TMPro namespace and public TextMeshProUGUI scoreText;
and I was able to drag and drop the Text (TMP) to the script as below:
using TMPro;
using UnityEngine;
public class className : MonoBehaviour
{
// Start is called before the first frame update
public TextMeshProUGUI scoreText;
void Start()
{
scoreText.text = "Score: "+ points;
}

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.

In unity C# script why Input is not exist?

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

Categories