- Home /
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");
}
}
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");
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");
}
}
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).
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
does the text mesh also have "DontDestroyOnLoad"?
If it does, then it should work.
If not, then you object is losing it's refernce.
The text mesh exist on the second scene. This gameobject is transfered to the last scene only
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
Follow this Question
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