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
1
Question by Josh707 · Jul 14, 2012 at 11:47 PM · stringreadother

Read string from other script (C#)

I have one C# script named Anims and I have this public string:

public string CurrentWeapon = null;

In the update function, I have that string changed to different values like "Fists" and other things, on button down.

How would I go about checking if CurrentWeapon is equal to certain things, in a completely separate script? I've tried getcomponent and other ways but none have worked. This seems like it should be the simplest thing to do.

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

1 Reply

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

Answer by aldonaletto · Jul 15, 2012 at 12:00 AM

That's because you may have many instances of the Anims script attached to different objects, thus you need a reference to the desired object (gameObject, transform, collider etc.) in order to get the right instance. The simplest way is to have a variable of type Anims (the script type) in each script that you want to access CurrentWeapon, and drag the object to which Anims is attached to them in the Inspector, like this:

 public Anims animsScript; // drag the Anims owner object here
 
   // you can read animsScript.CurrentWeapon directly, like this:
 
   string curWeapon = animsScript.CurrentWeapon;

If there's only one object in scene with the script Anims, another possibility is to find the desired object with GameObject.Find("objectName") or with GameObject.FindWithTag("objectTag") in Start, and use this reference with GetComponent when necessary - for instance:

 GameObject player;
 
 void Start(){
   player = GameObject.FindWithTag("Player");
 }
 
 // to read CurrentWeapon, use this:
 
   string curWeapon = player.GetComponent<Anims>().CurrentWeapon;

This approach is better when you don't want or just can't drag the Anims owner to each script where it will be needed.

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 Josh707 · Jul 15, 2012 at 12:28 AM 0
Share

I had it attached to only one object. Thanks for including instructions for both, it works!

avatar image maaarcooose · Sep 05, 2014 at 12:28 PM 0
Share

This is exactly what I was looking for.

Thanks.

avatar image thomasmking1990 · Oct 09, 2017 at 05:23 AM 0
Share

I tried doing this but I'm still getting this error: Cannot implicitly convert type 'string' to 'otherScriptName'.:

public class $$anonymous$$usic$$anonymous$$ain : $$anonymous$$onoBehaviour {

 public Object[] my$$anonymous$$usic; // music array
 public string songTitle; // The name of current song
 public AudioClip currentSong; // Whatever the current song playing is

 void Awake () {
     my$$anonymous$$usic = Resources.LoadAll("$$anonymous$$usic",typeof(AudioClip)); // load music in folder "Resources/$$anonymous$$usic"
     currentSong = my$$anonymous$$usic[Random.Range(0,my$$anonymous$$usic.Length)] as AudioClip;
     GetComponent<AudioSource> ().clip = currentSong;
 }

 void Start (){
     GetComponent<AudioSource>().Play();
     songTitle = currentSong.ToString();
     Debug.Log ("Song title is " + currentSong);
 }

 // Update is called once per frame
 void Update () {
 } // end update

} // end class

The class trying to access the 'songTitle' string:

public class JukeBoxNowPlaying : $$anonymous$$onoBehaviour {

 public $$anonymous$$usic$$anonymous$$ain songTitle;
 public string currentSong;
 //Text$$anonymous$$eshPro _songTitleText;
 //string[] _textCharacter;
 // Use this for initialization
 void Start () {
     songTitle = GameObject.Find ("stereoSource (1)").GetComponent<$$anonymous$$usic$$anonymous$$ain>().songTitle;
     currentSong = songTitle.ToString ();
     Debug.Log ("The song title (in Juke Box) is: " + currentSong);
 }
 
 // Update is called once per frame
 void Update () {
 }

}

avatar image aldonaletto thomasmking1990 · Oct 20, 2017 at 03:04 AM 0
Share

It seems that the error is elsewhere - there's no reference to "otherScriptName" in these scripts you've posted.

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

6 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Get GameObject name from other object. 2 Answers

Read from other script C# 1 Answer

calculate terms in runtime 3 Answers

Taking data from text file 2 Answers

PlayerPrefs.GetString returns wrong value 3 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