- Home /
how i can write a text from another code ?
how i can write a text from another code because text in the data
the first code in shop scene
using System.Collections; using System.Collections.Generic; using UnityEngine.SceneManagement; using UnityEngine.UI; using UnityEngine;
public class Buy_H : MonoBehaviour {
public GameObject CrystalsText;
void Start()
{
DataManegement.datamanegement.LoadData();
CrystalsText.GetComponent<Text>().text = DataManegement.datamanegement.AllCrystals.ToString() + " ";
}
public void BackToGame()
{
SceneManager.LoadScene("Main");
}
public void BUY_H()
{
if (DataManegement.datamanegement.AllCrystals < 20)
{
Debug.Log("ﺔﻴﻓﺎﻛ ﺮﻫﺍﻮﺟ ﻚﻳﺪﻟ ﺲﻴﻟ");
}
if (DataManegement.datamanegement.AllCrystals > 19)
{
DataManegement.datamanegement.AllCrystals -= 20;
}
}
public void BUY_L()
{
if (DataManegement.datamanegement.AllCrystals < 75)
{
Debug.Log("ﺔﻴﻓﺎﻛ ﺮﻫﺍﻮﺟ ﻚﻳﺪﻟ ﺲﻴﻟ");
}
if (DataManegement.datamanegement.AllCrystals > 74)
{
DataManegement.datamanegement.AllCrystals -= 75;
}
}
}
the another code in another scene ***
using System.Collections; using System.Collections.Generic; using System; using System.Runtime.Serialization.Formatters.Binary; using System.IO; using UnityEngine;
public class DataManegement : MonoBehaviour {
public static DataManegement datamanegement;
//my variables
public int tokensHighScore;
public int TotalTokensCollcted;
public int AllCrystals;
void Awake()
{
Environment.SetEnvironmentVariable("MONO_REFLECTION_SERIALIZER", "yes");
datamanegement = this;
DontDestroyOnLoad (gameObject);
}
public void SaveData()
{
BinaryFormatter binFrom = new BinaryFormatter();
FileStream file = File.Create(Application.persistentDataPath + "gameInfo.dat");
gameData data = new gameData();
data.tokensHighScore = tokensHighScore;
data.TotalTokensCollcted = TotalTokensCollcted;
data.AllCrystals = AllCrystals;
binFrom.Serialize(file, data);
file.Close();
}
public void LoadData()
{
if (File.Exists(Application.persistentDataPath + "gameInfo.dat"))
{
BinaryFormatter binForm = new BinaryFormatter();
FileStream file = File.Open(Application.persistentDataPath + "gameInfo.dat", FileMode.Open);
gameData data = (gameData)binForm.Deserialize(file);
file.Close();
tokensHighScore = data.tokensHighScore;
TotalTokensCollcted = data.TotalTokensCollcted;
AllCrystals = data.AllCrystals;
}
}
} [Serializable] class gameData { public int tokensHighScore; public int TotalTokensCollcted; public int AllCrystals; }
Answer by tormentoarmagedoom · Sep 13, 2017 at 02:06 PM
Good day @Haeder2132 !
I see you are little lost my friend :D
Lets make a quick masterclass.
First, you need to declare & define all variables you will use. A variable can be a integrer, a string, but also can be a GameObject, or a Component of a GameObject (like a text, a transform, a script, a image, a button...). All this "variable types" are called class.
So, first you need to declare this variables. Let's say you have aGameObject called CrystalsText that have a Text Component. Lets declare them:
GameObject ObjectCT; //This means "ObjectCT" is a GameObject
Text TextOfCT; //This means "TextOfCT" is a Text component
Now that is declared, you need to define them.
ObjectCT=GameObject.Find("CrystalsText");
TextOfCT = ObjectCT.GetComponent<Text>();
// Now you can do things like:
TextOfTC.text = "Hello! I'm a text";
But as we said, a variable can be a Script (is a component of a object). Lets say there is a script called CrystalScript in the CrystalText Object. First, delcare, like always, type and name we give to this variable. The type of the variable is the name of the script, because a script is a class!
CrystalScript ScriptOfCrystals;
Then define.
ScriptOfCrystals = ObjectCT.GetComponent<CrystalScript >();
We can also do the declarationa & definition in one line (but i don't recommend yet because sometime is not a good solution):
CrystalScript ScriptOfCrystals = ObjectCT.GetComponent<CrystalScript >();
Now lets talk about your problem. Imagine you are in a Script that needs to read a float variable called points in the CrystalScript placed in the ObjectCT
In this new script you need as always define and declare the script where you will find the variable you need:
CrystalScript ScriptOfCrystals = ObjectCT.GetComponent<CrystalScript >();
Then you can access that script and read all PUBLIC variables. For example lets print at the console the points.
Debug.Log ("You have " + ScriptOfCrystals.points + " points");
Well, i explained you some basic stuff. I expect it helps you :D Ask for more concrete questions if want good explanations using @tormentoarmagedoom :D
Rememeber to Upvote!
GameObject.Find is inefficient. Its better to drag and drop the game-object or use GameObject.FindGameObjectWithTag(). Also why have you declared a transform?
Was just an example. I delete it to prevent confusions
because i am had a crystaltext in another script in another scene and my text in canvas and i had a error here CrystalsText.GetComponent().text = Data$$anonymous$$anegement.datamanegement.AllCrystals.ToString() + " "; for Data$$anonymous$$anegement is another code
Answer by OusedGames · Sep 13, 2017 at 01:25 PM
Hello buddy, that's simple!
You need to get the text component an change it's text string, but both classes need to exist when you're are setting the string
Make you CrystalText variable a Type of Text instead of type GameObject
Then get the class reference, access the text variable and change the string value
Hope it works
CrystalsText.GetComponent<Text>().text = Data$$anonymous$$anegement.datamanegement.AllCrystals.ToString() + " ";
This piece of code seems correct, as long as there is a Text component on the crystals text game object and that you referenced UnityEngine UI.
using UnityEngine.UI;
Your answer
Follow this Question
Related Questions
how i save that in my data in load or save 2 Answers
How to delete a specific part of a text word? 1 Answer
Staring a 2D game 2 Answers
2D Draw text at sprite position with script only 1 Answer
Why wont my text box stay the same size? 2 Answers