Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 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
0
Question by smirlianos · Oct 06, 2013 at 11:13 AM · scenescoreleveldontdestroyonloadtext mesh

GetComponent doesn't work

Hello,

I make a game where after a set amount of time scene changes. The new scene should modify a text mesh to present the score of the older scene. I have this script but it doesn't work, any help??

 var scoreHold : int;
 
 function Awake () {
     DontDestroyOnLoad (transform.gameObject);
     if(GameObject.Find("TextScore"))
     {
         var textandpoints : TextMesh = GameObject.Find("TextScore").GetComponent("TextMesh");
         textandpoints.text = ("Example");
     }
 }
 
Comment
Add comment · Show 1
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 vexe · Oct 06, 2013 at 12:59 PM 0
Share

Which part of it doesn't work? Put a couple of logs to see what's going on. (and btw, why are you searching twice? search for it once and store it in a variable) (and use the generic version of GetComponent) and no need to do DontDestroyOnLoad(transform.gameObject); just DontDestroyOnLoad(gameObject);

  var textScore = GameObject.Find("TextScore")
  if(textScore != null)
  {
     print ("FOUND TextScore");
     var mesh = textScore.GetComponent<Text$$anonymous$$esh>();
     if (mesh != null)
     {
         print ("Everything should be O$$anonymous$$?");
         mesh.text = "example";
     } else print ("problem with mesh");
 
  } else print ("problem with finding the object");

2 Replies

· Add your reply
  • Sort: 
avatar image
0
Best Answer

Answer by aldonaletto · Oct 06, 2013 at 01:04 PM

Awake is executed only once, when the object is created. If you want to do something with TextScore whenever the scene changes, place DontDestroyOnLoad in a TextScore script and put the rest of the code in any other object - the object will be created when the scene starts and execute its Start function:

TextScore script:

 var scoreHold : int;
 
 function Awake () {
     DontDestroyOnLoad (transform.gameObject);
 }

Script attached to any other object:

 function Start(){
     var txtScore = GameObject.Find("TextScore");
     if (txtScore){
        var textandpoints : TextMesh = txtScore.GetComponent(TextMesh);
        textandpoints.text = ("Example");
     }
 }
Comment
Add comment · 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
0

Answer by dorpeleg · Oct 06, 2013 at 12:47 PM

It's because the Awake function only happens once.

The Awake is called when the object is first placed on the scene.

If you use "DontDestroyOnLoad" then the objects stays where it is when changing scenes.

Awake does not happened again on each scene change (same goes for Start).

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 smirlianos · Oct 06, 2013 at 12:51 PM 0
Share

But this is the purpose of this gameobject!

It modifies a textmesh(on the very start of the scene) so the player can see his score

avatar image dorpeleg · Oct 06, 2013 at 01:01 PM 0
Share

does the text mesh also have "DontDestroyOnLoad"?

If it does, then it should work.

If not, then you object is losing it's refernce.

avatar image smirlianos · Oct 06, 2013 at 01:21 PM 0
Share

The text mesh exist on the second scene. This gameobject is transfered to the last scene only

avatar image dorpeleg · Oct 06, 2013 at 01:38 PM 0
Share

So you will have to put your "GameObject.Find" on a different function (not Awake).

Because right now, what happnes, is this:

First scene:

Your gameobject is doing "GameObject.Find" and not finding anything.

Second scene:

Your gameobject is not doing "GameObject.Find", so its not finding your game object.

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

19 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

Related Questions

Cannot retrieve score from previous scene/level 1 Answer

DontDestroyOnLoad cancelling when Main Menu is open 1 Answer

How to make a saved score destroy on load new scene 0 Answers

Loading Game Object from one scene to AR scene 0 Answers

Character duplicates herself when re-entering the home level 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