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 Razputin · Jun 07, 2014 at 03:28 AM · errorrandomstringseed

Can't access string in another script

I'm trying to let the user choose which seed the game will load as their level, but I can't access the variable for some reason. What am I doing wrong??

 public class SeedNumberScript : MonoBehaviour {
 
      public string stringToEdit = "Enter A Seed Number";
 
     void OnGUI() {
          stringToEdit = GUI.TextField(new Rect(10, 10, 200, 20), stringToEdit, 25);
 
         if(GUI.Button(new Rect(10,400,1200,100), "Enter The World"))
         {
                 
                 Application.LoadLevel("NewIdeas&Tests");
             }
         }
     }



Second Script

 void Start () {
 
         SeedNumberScript.stringToEdit == Random.seed;
 
         for(int z = 0; z < GameHeight; z++)
             {
                 for(int x = 0 ; x < GameWidth; x++)
                 {
                     float rnd = Random.value;
                     if(rnd <0.25f)
                     {
                         Instantiate(TileOne, new Vector3(x,0,z), Quaternion.identity);
                     }
                     else if(rnd <0.5f)
                     {
                         Instantiate(TileTwo, new Vector3(x,0,z), Quaternion.identity);
                     }
                     else
                     {
                         Instantiate(TileThree, new Vector3(x,0,z), Quaternion.identity);
                     }
                 }
             }
         }
     }


I'm trying to access the String in the first scrip with the second script. I keep getting Only assignment, call, increment, decrement, and new object expressions can be used as a statement

Comment
Add comment
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

2 Replies

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

Answer by oasisunknown · Jun 07, 2014 at 03:34 AM

First I dont see a reference declared to the other script. try here for more information on that.

http://docs.unity3d.com/412/Documentation/ScriptReference/index.Accessing_Other_Components.html

but also make sure your doing the right call.

 SeedNumberScript.stringToEdit == Random.seed;

the double == means check to see if this is THE SAME AS Random.Seed;

 SeedNumberScript.stringToEdit = Random.seed;

This says SET .stringToEdit to Random.Seed;

Comment
Add comment · Show 7 · 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 Razputin · Jun 07, 2014 at 03:45 AM 0
Share

Thanks for the responses guys since eric already had an unholy amount of karma ill give You best answer. He could drown in all his o.o

avatar image oasisunknown · Jun 07, 2014 at 03:47 AM 0
Share

I appreciate that. but I am curious on which version of == Random.Seed did you need to use?

or was it just the GetComponent method that was needed?

avatar image Razputin · Jun 07, 2014 at 03:22 PM 0
Share

Ill give you an update in a bit. Haven't had a chance to do it yet, had to go to bed then work.

avatar image Razputin · Jun 07, 2014 at 08:38 PM 0
Share

I did need just one = because I no longer get an error on that line. I also need to get the component tho because its telling me an object reference is required. So then it looks like this

 GetComponent<SeedNumberScript>().stringToEdit;
 SeedNumberScript.stringToEdit = Random.seed;


but that gives me another Only assignment, call, increment, decrement, and new object expressions can be used as a statement error on the .stringToEdit; Did I set up the string wrong?

avatar image oasisunknown · Jun 07, 2014 at 09:57 PM 0
Share

technically your not doing something wrong but look at it this way.

your saying take my string and make it an integer. your trying to fundamentally change the type of the string.

I don't know what your using the string for but if you want it to just display the number try.

 SeedNumberScript.stringToEdit = "" +Random.seed;

this concatenates the int on the end of a string that has nothing in it. and that is acceptable in unity.

for more reference look up subjects on casting to different variable types.

https://www.youtube.com/watch?v=CVx6W2OdvLU∈dex=4&list=PL4BBB74C7D2A1049C

in this video around $$anonymous$$ute 18 he talks about specific cast operations in java but the concept still applies.

Show more comments
avatar image
2

Answer by Eric5h5 · Jun 07, 2014 at 03:30 AM

You need to use GetComponent.

 GetComponent<SeedNumberScript>().stringToEdit....


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

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

23 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 avatar image avatar image avatar image avatar image

Related Questions

Script not working (Java script) 2 Answers

can your Random.Range parameters match with function perameter (javascript) 2 Answers

The name 'Joystick' does not denote a valid type ('not found') 2 Answers

String.Split problem 1 Answer

How to make particles randomized each run? 1 Answer


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