Referencing variables in another script on the same game object
I've done some digging around and tried out what I found but it doesn't seem to be working.
I'm making a pong game and would like the outcome of the match to affect the text that appears in the next scene.
I assigned my script that counts a player's score to the ball, and also to the camera in the screen I would like the text to appear. In another script attached to the same camera, I tried to use the players' scores in "if" statements, but it isn't working. Could someone help me?
Here's the code in question:
void Start ()
{
int score1 = GetComponent<Camera>().GetComponent<ball>().score1;
int score2 = GetComponent<Camera>().GetComponent<ball>().score2;
GameObject.Find ("textoption1").transform.localScale = new Vector3(0, 0, 0);
GameObject.Find ("textoption2").transform.localScale = new Vector3(0, 0, 0);
//This makes the text options invisible
StartCoroutine(ExecuteAfterTime());
}
IEnumerator ExecuteAfterTime()
{
int score1 = GetComponent<Camera>().GetComponent<ball>().score1;
int score2 = GetComponent<Camera>().GetComponent<ball>().score2;
if (score1 == 10) {
GameObject.Find ("pmone1p1").transform.localScale = new Vector3 (1, 1, 1);
yield return new WaitForSeconds (5);
GameObject.Find ("pmone1p1").transform.localScale = new Vector3(0, 0, 0);
}
if (score2 == 10) {
GameObject.Find ("pmone1p2").transform.localScale = new Vector3 (1, 1, 1);
yield return new WaitForSeconds (5);
GameObject.Find ("pmone1p2").transform.localScale = new Vector3(0, 0, 0);
}
}
Thanks!
Just noticed that where it says "pmone1p1" and "pmonep2" it's supposed to be "textoption1" and "textoption2" but I only changed that for posting it on here. It isn't like that in my actual script, so that's not the problem. Just wanted to mention that before someone pointed it out.
Can you be a bit more specific about what's going wrong? One thing I noticed is you get the score from the Start() function but typically the score will be 0 at the start of most games.
The game of pong itself takes place in one scene, the scene 2 to be specific, and this code is supposed to execute in the following scene, scene 3. As such, it is attached to both the ball, and a game object that only appears in scene 3, namely the default camera that is there from the start whenever you make a new scene.
The script that keeps track of the players' scores is on the ball that's used in the game in scene 2. The script is called "ball." I attached the script "ball" to the camera, which I renamed from "$$anonymous$$ain Camera" to "Camera." There are no error messages and the game runs, but the code doesn't execute properly. In start, I set the text game objects to size (0,0,0), effectively shrinking them into nothing. From there it runs the function that has a delay in it. (The reason I have this function is that there's another text object that's supposed to reappear after a certain amount of time, but I didn't include it here because it's not really relevant.) Anyway, the idea is that the script takes the variables "score 1" and "score 2" from the script "ball" and uses them in the if statements. If player 1 won the match (if "score 1" equals 10, since the game ends when one player has 10 points) the text reflecting this is supposed to appear again by having its size set back to normal. The same is meant to happen for text option 2 if player 2 wins and score 2 equals 10.
However, the text is staying invisible. Its size is not being set back to normal. What I imagine is happening is, for some reason, when score1 and score2 are pulled from "ball," neither of them are 10. As a result, neither if statement is executed, leaving the text small. I haven't had a chance to test this yet, but I think I may have a solution. In "ball," score 1 and score 2 are set to 0 in the "start" function. By the end they're 10, because that's the trigger for loading the next scene and that works fine. But if this script is pulling the initial values of score 1 and 2 from "ball" then that would explain this issue. Does that seem right?
Sorry for not being more specific from the start, when I posted this I was short on time.
I assigned my script that counts a player's score to the ball, and also to the camera in the screen I would like the text to appear.
By 'screen' do you mean a different scene? I'm not getting a clear picture of the whole problem, but having the same script in 2 different scenes doesn't mean they share any information.
@nosekills is right a scene has it's own data and you can't pass information from scene to scene as you can from script to script.
Watch the tutorial on data persistence and it'll show you ways of passing information from one scene to another.
Your answer
![](https://koobas.hobune.stream/wayback/20220612081635im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
Problem while using several tags in a OnTriggerEnter (Collider other) 0 Answers
If statement unexpectedly changing value. 0 Answers
Unity quest system a better way? 1 Answer
Need a object to react different when clicked mutliple times 1 Answer
Cannot assign variable agents for clicking a button 0 Answers