Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
3
Question by Haeder2132 · Sep 13, 2017 at 12:14 PM · 2dtextscenebeginnerdata

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

Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

2 Replies

· Add your reply
  • Sort: 
avatar image
1

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!

Comment
Add comment · Show 4 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Haeder2132 · Sep 13, 2017 at 03:56 PM 0
Share

that's dont work for me

avatar image kaplica · Sep 13, 2017 at 03:56 PM 0
Share

GameObject.Find is inefficient. Its better to drag and drop the game-object or use GameObject.FindGameObjectWithTag(). Also why have you declared a transform?

avatar image tormentoarmagedoom kaplica · Sep 13, 2017 at 03:59 PM 0
Share

Was just an example. I delete it to prevent confusions

avatar image Haeder2132 · Sep 13, 2017 at 04:01 PM 0
Share

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

avatar image
-1

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

Comment
Add comment · Show 4 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Header-Mohammed · Sep 13, 2017 at 01:30 PM 1
Share

how i am a beginner

avatar image Haeder2132 · Sep 13, 2017 at 01:42 PM 0
Share

how i am a beginner and i don't know how ?

avatar image kaplica · Sep 13, 2017 at 01:49 PM 0
Share
 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;

avatar image Haeder2132 kaplica · Sep 13, 2017 at 03:47 PM 0
Share

i make it but in don't work

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

143 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

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


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges