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 /
avatar image
0
Question by mpina · Oct 31, 2017 at 07:00 AM · scripting problemscoreendgame

Ending a game with Points

Hey Guys, I'm pretty new to Unity and need some help. So I'm creating a game where the player has to collect Orbs in order to go through this wall of light and end the game. They need to collect at least 6 orbs in order to finish the game and get transported back to the main menu.

Here's my script for the ending:

 using System.Collections;
 using System.Collections.Generic;
 using UnityEngine;
  
 public class End : MonoBehaviour
 {
     public CollectOrb orbs;
  
     // Use this for initialization
     void Start()
     {
        
     }
  
     // Update is called once per frame
     void Update()
     {
  
     }
  
     void OnTriggerEnter(Collider other)
     {
         Debug.Log("OnTriggerEnter() was called " + other.gameObject);
         if (orbs.score == 1) //testing with one orb
         {
             Debug.Log("now free to leave");
             Application.LoadLevel("Menu");
         }
         else
         {
             Debug.Log("Keep Movin' pal");
         }
  
     }
  
 }

Basically, I need help between making the two scripts (End and CollectOrb) communicate with each other. I thought I did it right by having the orb script call upon score, but every time the game gets to that if statement it prints "NullReferenceException: Object reference not set to an instance of an object End.OnTriggerEnter (UnityEngine.Collider other) (at Assets/Our Scripts/End.cs:24)"

Here's CollectOrb as well:

 using System.Collections;
 using System.Collections.Generic;
 using UnityEngine;
  
 public class CollectOrb : MonoBehaviour {
  
     public int score = 0;
  
     // Use this for initialization
     void Start () {
        
     }
    
     // Update is called once per frame
     void Update () {
        
     }
  
     void OnTriggerEnter(Collider other)
     {
         Debug.Log("OnTriggerEnter() was called "+ other.gameObject);
         Debug.Log("Object is an orb");
         Destroy(gameObject, 0.01f);
         score++;
         Debug.Log("Score is now " + score);
     }
 }

Thank you so much for your time!!

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

Answer by Hellium · Oct 31, 2017 at 07:06 AM

Remove your End script and attach the following instead :

  using System.Collections;
  using System.Collections.Generic;
  using UnityEngine;
   
  public class GameManager : MonoBehaviour
  {
     private int score ;
     
     public int Score
     {
         get { return score ;}
         set
         {
             if( value > 0 )
                 score = value ;
                 
             Debug.Log( "The new score is : " + score ) ;
             if( score == 5 )
             {
                 Debug.Log("now free to leave");
                 Application.LoadLevel("Menu");
             }
         }
     }  
  }

Attach the following script to your Orb, and drag & drop the gameobject holding your Gamemanager script into the gamemanager field in the inspector

 using System.Collections;
 using System.Collections.Generic;
 using UnityEngine;
  
 public class Orb : MonoBehaviour
 {
    public GameManager gameManager ;
 
    void OnTriggerEnter(Collider other)
    {
        Debug.Log("OnTriggerEnter() was called "+ other.gameObject);
        Debug.Log("Object is an orb");
        gameManager.Score++;
        Destroy(gameObject, 0.01f);
    }
 }
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 mpina · Oct 31, 2017 at 05:01 PM 0
Share

Thanks a ton! This helped me a lot.

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

127 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

Related Questions

My score script doesn't seem to work properly, score is not added properly 0 Answers

Central score counter for 3 score generating buttons 1 Answer

INCREASE SCORE MORE AND MORE? 1 Answer

Score counter breaking after adding points 2 Answers

Can't increase my score after OnTriggerEnter occurs. 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