I have been following a tutorial for saving player names with playerprefs.
I got it working normal text, but not with TextMeshPro. Is there a way to edit the code so that I can use TextMeshPro with the scripts?
First Script
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
public class SaveName : MonoBehaviour
{
public InputField textBox;
public void ClickSaveButton()
{
PlayerPrefs.SetString("name", textBox.text);
Debug.Log("Your name is " + PlayerPrefs.GetString("name"));
}
}
Second Script
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
public class NameScene : MonoBehaviour
{
public Text NameBox;
void Start()
{
NameBox.text = PlayerPrefs.GetString("name");
}
// Update is called once per frame
void Update()
{
}
}
Import TextMeshPro to your script and instead of public Text NameBox; use public TMP_Text NameBox;
Related
I'm making a 2D game and want to jump from a dialogue scene that is a tutorial for the beginning of the game to a scene where the game starts but I don't know how to achieve that after the dialogue ends. here is the entire script for the scene ( dialogue manager, Dialogue, and Dialogue trigger).
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
using TMPro;
public class DialogueManager : MonoBehaviour
{
public TextMeshProUGUI nameText;
public TextMeshProUGUI dialogueText;
private Queue<string> sentences;
void Start()
{
sentences = new Queue<string>();
}
public void StartDialogue(Dialogue dialogue)
{
nameText.text = dialogue.name;
sentences.Clear();
foreach (string sentence in dialogue.sentences)
{
sentences.Enqueue(sentence);
}
DisplayNextSentence();
}
public void DisplayNextSentence()
{
if (sentences.Count == 0)
{
EndDialogue();
return;
}
string sentence = sentences.Dequeue();
dialogueText.text = sentence;
}
void EndDialogue()
{
Debug.Log("End of conversation.");
}
}
And this is the Dialogue Script:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
[System.Serializable]
public class Dialogue
{
public string name;
[TextArea(3, 10)]
public string[] sentences;
}
And this is the Dialogue Trigger:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class DialogueTrigger : MonoBehaviour
{
public Dialogue dialogue;
public void TriggerDialogue()
{
FindObjectOfType<DialogueManager>().StartDialogue(dialogue);
}
}
Use SceneManager.LoadScene in your EndDialogue() method
void EndDialogue()
{
// load new scene here
Debug.Log("End of conversation.");
}
I am making a game where, if the catcher gets destroyed by the object, the game over screen is triggered. All that seems to occur is that there is a giant game over the screen at the beginning of when I play, while the game is running in the background. For some reason, the game does not seem to call in the game over screen only on collision.
This is the script I am using for my catcher, where it collides, disappears, and then the game over screen is set up to be triggered.
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class CatcherDestroy : MonoBehaviour
{
public GameOverScreen GameOverScreen;
private void OnCollisionEnter2D(Collision2D collision)
{
if (collision.gameObject.CompareTag("Catcher"))
{
Destroy(collision.gameObject);
GameOverScreen;
}
}
}
and this is the code for my GameOverScreen.
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
public class GameOverScreen : MonoBehaviour
{
public Text pointsText;
public void Setup(int score)
{
gameObject.SetActive(true);
pointsText.text = "Score:" + score.ToString();
}
}
You are not calling anything when an object collides, you're just listing the reference of the object. You'll need to call the function that you have exposed. Without calling a method from the script reference, no code will be run. Edit your first snippet as follows:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class CatcherDestroy : MonoBehaviour
{
public GameOverScreen GameOverScreen;
private void OnCollisionEnter2D(Collision2D collision)
{
if (collision.gameObject.CompareTag("Catcher"))
{
Destroy(collision.gameObject);
GameOverScreen.Setup();
}
}
}
The other option would be to move the code in Setup to Awake or Start or OnEnable, then instead of calling the function in the collision, you just need to set it as active.
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class CatcherDestroy : MonoBehaviour
{
public GameOverScreen GameOverScreen;
private void OnCollisionEnter2D(Collision2D collision)
{
if (collision.gameObject.CompareTag("Catcher"))
{
Destroy(collision.gameObject);
GameOverScreen.score = theScore;
GameOverScreen.gameObject.SetActive(true);
}
}
}
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
public class GameOverScreen : MonoBehaviour
{
public Text pointsText;
public int score;
private void OnEnable()
{
pointsText.text = "Score:" + score.ToString();
}
}
The one issue is you'll need to pass in the score parameter which I do not see in your script CatcherDestroy.
I am trying to make a script that will take in a list of animations and automatically add them to the blend tree. I've had a look at the blend tree API and came up with this:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEditor;
using UnityEditor.Animations;
public class AnimationSwitcher : MonoBehaviour
{
public List<Motion> animations;
public Animator animator;
public BlendTree blendTree;
public void addMotions()
{
for (int i = 0; i < animations.Count; i++)
{
blendTree.AddChild(animations[i]);
}
}
}
I know that I have to use the animator component, but I can't get a easily readable answer on how I can use it to add the blendtree children. I've tried assigning the blendtree in the editor, but that didn't work either
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEditor;
using UnityEditor.Animations;
public class AnimationSwitcher : MonoBehaviour
{
public List<Motion> animations;
public AnimatorController animator;
private BlendTree blendTree;
void Start()
{
animator.CreateBlendTreeInController("Active", out blendTree);
blendTree.AddChild(animations[0]);
}
public void addMotions()
{
for (int i = 0; i < animations.Count; i++)
{
blendTree.AddChild(animations[i]);
}
}
}
I'm making a 2d game on unity, and I have a script that saves my coin value, but it doesn't save when I open the after getting more coins.
This is my script that saves the data:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class Coin : MonoBehaviour
{
private void OnTriggerEnter2D(Collider2D other)
{
if(other.gameObject.tag == "Player")
{
PlayerPrefsManager.coins += 1;
PlayerPrefsManager.UpdateCoins();
}
}
}
And my script that displays the data:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
public class CoinsDisplay : MonoBehaviour
{
private Text text;
void Start()
{
text = GetComponent<Text>();
}
void Update()
{
string[] temp = text.text.Split('X');
text.text = temp[0] + "X: " + PlayerPrefsManager.coins;
}
}
player prefs manager script:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class PlayerPrefsManager : MonoBehaviour
{
public const string Coins = "Coins";
public static int coins;
void Start()
{
coins = PlayerPrefs.GetInt("Coins");
}
public static void UpdateCoins()
{
PlayerPrefs.SetInt("Coins", coins);
coins = PlayerPrefs.GetInt("Coins");
PlayerPrefs.Save();
}
}
Your code should work fine, but I guess you've forgotten to assign your PlayerPrefsManager script to a gameObject. Cause if its Start doesn't execute, we will get the same result.
By the way, use your const string 'Coins', you've just declared it but you're not using it in these calls: SetInt, GetInt.
And try to update PlayerPrefs (PlayerPrefs.Save()) less often, since it writes data on the disk and is a little expensive to do it each time your coin's value changes. You can have a method called 'save' on your prefManager to only get called when you're done with the level, mission is complete etc... , or if you don't want to lose the data, call it less often, like once in each 10 seconds (take a look at Invokerepeating)
Basically, I have this Text (scoreText) which is in my "Menu" scene so hence I have initiated it in GameControlMenu.cs, however, I'm trying to change its text from my other script GameControl.cs whilst I'm currently on my "Main" scene.
GameControlMenu.cs:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
using UnityEngine.SceneManagement;
public class GameControlMenu : MonoBehaviour
{
public static GameControlMenu instanceMenu;
public Text scoreText;
void Start()
{
//does stuff but not important to question
}
}
GameControl.cs:
using UnityEngine;
using System.Collections;
using UnityEngine.UI;
using UnityEngine.SceneManagement;
using System;
using System.Collections.Generic;
using System.Threading;
using System.Threading.Tasks;
public class GameControl : MonoBehaviour
{
public static GameControl instance;
public int score = 5;
void Start()
{
GameControlMenu.instanceMenu.scoreText.text = "PREVIOUS SCORE: " + score;
}
}
This setup works for a couple of my other variables which I'm accessing from another file but for whatever reason this just keeps throwing me the error: NullReferenceException: Object reference not set to an instance of an object
Any help is appreciated :)
You can't do GameControlMenu.instanceMenu... since GameControlMenu is in another scene as you described, and the instance isn't in the current scene.
But what you can do is to store the value somewhere first, and let the GameControlMenu use it when the other scene loads like so:
GameControlMenu.cs
public class GameControlMenu : MonoBehaviour
{
public static GameControlMenu instanceMenu;
public static string StuffToShowOnScoreText { get; set; }
public Text scoreText;
void Awake()
{
// So that it loads the text on start
scoreText.text = StuffToShowOnScoreText;
// ...
}
}
GameControl.cs
public class GameControl : MonoBehaviour
{
public static GameControl instance;
public int score = 5;
void Start()
{
// Store the value
GameControlMenu.StuffToShowOnScoreText = "PREVIOUS SCORE: " + score;
}
}