- Home /
How to use gameobject that survived using awake?
I am using awake function to survive a gameobject when changing scene(another level of my game).Now how can I use the variable stored with that game object? I am using
var obja : GameObject = GameObject.Find("Score");
function Score()
{
score=obja.GetComponent(CalcScore).score+currentscore;
}
where currentscore is score of current level . But it gives error "The variable obja of 'Script' has not been assigned", but I can manually assign the gameobject score to the script at runtime.And then it can calculate score properly.
Someone suggested to use singleton also,but I do not understand how to use it.
As a general re$$anonymous$$der, people who try to help you out appreciate it a lot when you use the site's voting system and/or actually click to accept something as the right answer, when an Answer helped you...
I cant click anywhere, it says you have no permission to do that.
Yes , I can do better. But I dont know how to do it. Can you please tell me how to format my code?
When you Edit your question, select the code and hit the 101-010 button.
Answer by roamcel · Aug 17, 2011 at 05:02 AM
I can't really say what's wrong from the code excerpt you wrote, but I suggest a test to figure out what to fix:
create a local CalcScore cache variable, and ASSIGN a value to 'score'
localcalcscore : CalcScore = obja.GetComponent(CalcScore);
localcalcscore.score = 999;
score = localcalcscore.score+currentscore;
if this raises an error, there's an object referencing issue (the object "Score" perhaps not really alive from the other scene) in the absence of errors, it means that CalcScore.score is really not initialized... so you probably aren't coming from the real scene where "Score" gets created.
Singletons are basically single-instance variables and classes, that don't need be instantiated with a gameobject and that exist project-wide.
They could be beneficial in your case. c# example:
using UnityEngine; using System.Collections;
//doesn't derive from monobehavior public static class mystaticclass { public static float globalscore = 0; }
then you can access the static value with a simple
mystaticclass.globalscore = 999;
Thanks but I get the error, I only have to use the gameobject.find("score") method inside the function Score so that it works at runtime.
"if this raises an error, there's an object referencing issue (the object "Score" perhaps not really alive from the other scene) in the absence of errors, it means that CalcScore.score is really not initialized... so you probably aren't co$$anonymous$$g from the real scene where "Score" gets created"
Also I would like to ask one more thing, if you can answer . I read a document where they told me just to create script files from project,but that do not help me create my own script files. Also if I create script files from monodevelop then they cannot get attached to unity.
ALL project files must be:
1 - created with the CREATE button in the PROJECT tab OR
2 - dragged from outside the project, into the PROJECT tab. OR
3 - imported (as packages or other assets)
PLUS: there's only three reasons I know of, regarding inability to attach a script to a gameobject:
1 - the class $$anonymous$$UST derive from monobehaviour
public class myclass
public class myderivedclass : $$anonymous$$onoBehaviour {
2 - there's compilation errors in the editor. you can attach a script only after it has compiled with no errors (warnings are no problem)
3 - in C# scripts, the class name $$anonymous$$UST be equal to the name of the file.
public class myderivedclass : $$anonymous$$onoBehaviour {
Your answer
![](https://koobas.hobune.stream/wayback/20220612182432im_/https://answers.unity.com/themes/thub/images/avi.jpg)