- Home /
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.
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.
I had it attached to only one object. Thanks for including instructions for both, it works!
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 () {
}
}
It seems that the error is elsewhere - there's no reference to "otherScriptName" in these scripts you've posted.
Your answer
Follow this Question
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