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
0
Question by rushikesh90 · Aug 17, 2011 at 03:37 AM · gameobjectscene

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.

Comment
Add comment · Show 5
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 rushikesh90 · Aug 17, 2011 at 03:38 AM 0
Share

Someone suggested to use singleton also,but I do not understand how to use it.

avatar image CHPedersen · Aug 17, 2011 at 08:19 AM 1
Share

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...

avatar image rushikesh90 · Aug 17, 2011 at 10:03 AM 0
Share

I cant click anywhere, it says you have no permission to do that.

avatar image rushikesh90 · Aug 17, 2011 at 10:15 AM 0
Share

Yes , I can do better. But I dont know how to do it. Can you please tell me how to format my code?

avatar image Waz · Aug 17, 2011 at 10:18 AM 0
Share

When you Edit your question, select the code and hit the 101-010 button.

1 Reply

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

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;
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 rushikesh90 · Aug 17, 2011 at 05:22 AM 0
Share

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.

avatar image roamcel · Aug 17, 2011 at 08:05 AM 0
Share

"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"

avatar image rushikesh90 · Aug 17, 2011 at 10:03 AM 0
Share

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.

avatar image roamcel · Aug 17, 2011 at 11:58 AM 0
Share

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

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

5 People are following this question.

avatar image avatar image avatar image avatar image avatar image

Related Questions

Calling function from another scenes gameobject 0 Answers

Load a scene/level when enemy is close... 1 Answer

how can I display a variable as a GUIText 5 Answers

Using empty gameobjects as "folders" in scene hierarchy panel 1 Answer

How to call a function from a script in another scene 5 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