• 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 aminn · Nov 18, 2014 at 12:51 PM ·

how to call script of other scene to current.

hi all, i am doing a project in unity3d.i have one doubt,can it possible to call a script from other scene to current scene.if so,how it would be?......please let me know..anybody pls help..

Comment
Add comment · Show 3
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 HarshadK · Nov 18, 2014 at 11:49 AM 0
Share

If you want to carry script between scenes then make that script as static and add it to a game object and set that object to be DontDestroyOnLoad.

avatar image fafase · Nov 18, 2014 at 12:55 PM 0
Share

Otherwise answer is no. When a new scene is loaded, everything from the previous is destroyed (Except those DontDestroyOnLoad objects). So calling an instance method from previous scene would result in null reference exception.

A static method on the other hand is acting different, as it belongs to the class, you do not need instance to call it but it won't have access to any non static variable.

avatar image aminn · Nov 18, 2014 at 01:05 PM 0
Share

k perfect,i will do so,tnx a lot

2 Replies

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

Answer by fafase · Nov 19, 2014 at 05:52 AM

Considering your latest comment, I would guess you are after what is called in a fancy way data persistence.

So there are different ways to do so.

DontDestroyOnLoad:

You add that method in a script and the object will survive reloading a scene

 private string myString;
 public string MyString{get{return myString;}}
 void Start()
 {
     DontDestroyOnLoad(gameObject);
 }

 

then any other script can take care of it:

 void Start(){
     // find the object and get the script
     string str = script.MyString;
     Destroy(theObject);
 }

Once you got your value you should get rid of the object if you do not need it since it will remain else just keep it.

PlayerPrefs:

Second way is to store the value in memory:

 string myString;
 void OnDestroy()
 {
      PlayerPrefs.SetString("Data", myString);
 }

in the other script:

 void Start()
 {
     string str = PlayerPrefs.GetString("Data");
 }


Static:

The latest is to use some static value:

 static class Data{
     public static string MyString{get;set;}
 }

Now here is the script containing the value:

 private string myString;
 void OnDestroy(){
     Data.MyString = myString;
 }

and in the other script:

 void Start(){
    string str = Data.MyString;
 }



Comment
Add comment · Show 2 · 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 aminn · Nov 19, 2014 at 01:29 PM 0
Share

well,thats something useful fafase :))...........i think it may work out..tnx a lot ....

avatar image meat5000 ♦ · Nov 19, 2014 at 01:32 PM 0
Share

$$anonymous$$ark the answer as Accepted if it does the trick for you :)

avatar image
0

Answer by nketc · Nov 18, 2014 at 03:20 PM

Go to the add component in inspector window, click scripts, and add any script that exist in your project. Either you are using it on any other scene or object.

alt text


screen shot 2014-11-18 at 6.45.57 pm 1.png (15.5 kB)
Comment
Add comment · Show 1 · 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 aminn · Nov 19, 2014 at 05:32 AM 0
Share

tnx a lot nketc...........:)...but the thing is...i have some objects in hierarchy to drag to appropriate position in script wchich coudnt be available in my current scene.....so how could it possible....i want to call string from the other script to my current script....can it possible through any way.

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.

Welcome to Unity Answers

If you’re new to Unity Answers, please check our User Guide to help you navigate through our website and refer to our FAQ for more information.

Before posting, make sure to check out our Knowledge Base for commonly asked Unity questions.

Check our Moderator Guidelines if you’re a new moderator and want to work together in an effort to improve Unity Answers and support our users.

Follow this Question

Answers Answers and Comments

3 People are following this question.

avatar image avatar image avatar image

Related Questions

A node in a childnode? 1 Answer

'Use SetTriangles instead. Internally this function will convert the triangle strip to a list of triangles anyway.' (27,74) & (130,73) & (177,30) 0 Answers

My game has low FPS on iPhone 5 but not Android... 0 Answers

Custom Pivot Points in Sprite Editor 1 Answer

Dynamic Path Generation for 2D 0 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