Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 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 /
  • Help Room /
avatar image
0
Question by grimaceaddict · May 27, 2016 at 01:14 AM · variablesif-statements

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!

Comment
Add comment · Show 5
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 grimaceaddict · May 26, 2016 at 05:00 PM 0
Share

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.

avatar image Mmmpies · May 27, 2016 at 10:52 AM 0
Share

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.

avatar image grimaceaddict Mmmpies · May 27, 2016 at 06:41 PM 0
Share

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.

avatar image NoseKills · May 27, 2016 at 02:31 PM 0
Share

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.

avatar image Mmmpies · May 27, 2016 at 08:02 PM 0
Share

@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.

0 Replies

· Add your reply
  • Sort: 

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

5 People are following this question.

avatar image avatar image avatar image avatar image avatar image

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


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