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
0
Question by TwitchyOrphan · Jan 26 at 08:38 PM · scripting problemscore

Incrementing score between two scripts

Hello! I have a game manager script and animal behavior script. I have my game winning conditions and effects set up on the game manager script to run once my score equals 3. The way to score is by pressing a button within a collision detection which sends the animal into the pen and I'd like to increment my score in the animal behavior script, but then access that score on the game manager script to tell the game if the player has won or not.

I've tried to reference the score from one script to the other, but it's not even letting me type it out. Sorry if more information is needed, but let me know if any specifics and I'll post more.

 public void WinGame()
 {
     //This is the Winning Script, but need to set it to an actual win condition instead of pressing Q
     if (score == 3)
     {
         if (!hasWon)
         {
             hasWon = true;
             blueFireworks.Play();
             greenFireworks.Play();
             yellowFireworks.Play();
             Cursor.lockState = CursorLockMode.None;
             winScreen.gameObject.SetActive(true);
         }

         if (hasWon)
         {
             return;
         }
     }
 }

 private void OnCollisionEnter(Collision other)
 {
     if (other.gameObject.CompareTag("Barrier"))
     {
         transform.Rotate(Vector3.up, 180);
     }

     if (other.gameObject.CompareTag("Player"))
     {
         if (Input.GetKey(KeyCode.E))
         {
             caughtSound.Play();
             GetCaught();
             //Need to increment score here, store it, and access it on the game manager script to run the WinGame method
         }
     }
 }

Again, WinGame() is in the GameManager script and OnCollisionEnter() is in the AnimalBehaviorScript. TLDR: How can I access a score that is incremented and saved in the AnimalBehaviorScript to determine if the game has been won and run my WinGame() on the GameManager script.

Thank you in advance and sorry if any confusion!

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
0
Best Answer

Answer by ImKyleConway · Jan 26 at 09:54 PM

Luckily you can keep the score in the GameManager script and have the Animal script update that instead. To start in order to reference the script you will need to set it in the script like:

 public GameManager gm; //The Same Location As Your Other Bools, Floats, etc.

 void Start()                                            
 { gm = GameObject.Find("GameManager").GetComponent<GameManager>(); } 

After that you should be able to access your score from the Animal Script. If your score is an int, in the same area as where you play the sound effect you could do something like:

 gm.score++;

This will add 1 point to your score every time it is run. If it is a float you could do:

 gm.score = gm.score + 1f;

I find it is much easier to stay organized allowing the Game Manager to handle things like keeping track of score. Just make sure the score in the game manager is public instead of private, otherwise unity wont be able to find it.

Comment
Add comment · Show 1 · 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 TwitchyOrphan · Jan 26 at 11:27 PM 0
Share

Awesome! Funny enough, this is very similar to my earlier solution, BUT I failed to realize (until after seeing your example) that I had the score variable in GameManager script set as private instead of public and that's why it wouldn't let me type it out /sigh

You da real MVP. Thank you so much @ImKyleConway !

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

235 People are following this question.

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

Related Questions

Score counter breaking after adding points 2 Answers

INCREASE SCORE MORE AND MORE? 1 Answer

Central score counter for 3 score generating buttons 1 Answer

Ending a game with Points 1 Answer

Scoring points problem 1 Answer


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