- Home /
Calling a Function from other script ( C# )
I want to refer a function wich is in an other script, but I not succeed. Here is the script, where I want to use the function "SetScoreText":
public class Basescore : MonoBehaviour
{
public int score;
void add1000()
{
score = score + 1000;
displayMain.SetScoreText();
}
And this is the script, wich contains the function itself:
public class displayMain : MonoBehaviour
{
public TextMesh displayTextScore ;
public class Basescore
{
public int score;
}
void SetScoreText ()
{
displayTextScore.text = score.ToString();
}
}
In the line "public class Basescore" I tried to call the variable "score", but It still miss it. (I am baginner in programming :) )
Thanks! Bálint
Answer by rlarocca92 · Nov 17, 2013 at 04:58 PM
Since displayMain extends MonoBehaviour, it should be applied to a game object. What you can then do is create a variable of type displayMain, to reference the displayMain script. Then you can call SetScoreText() from that reference.
public class Basescore : MonoBehaviour
{
public int score;
public displayMain display; /*reference to displayMain script*/
void add1000()
{
score = score + 1000;
display.SetScoreText(); /*call SetScoreText from reference*/
}
}
After you add this to your script, go back to your unity editor. Select the game object that has the Basescore script attached. In the inspector window, under the Basescore script, you should see the variable display. Drag the game object that has the displayMain script attached to it and assign it to your display variable.