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 /
This question was closed May 31, 2017 at 12:42 AM by ZachAttack9280 for the following reason:

Other

avatar image
0
Question by ZachAttack9280 · May 27, 2017 at 04:51 PM · c#nullreferenceexceptionnullnull reference exceptionnullreference

NullReferenceException: Object reference not set to an instance of an object ProgressBar.Start ()

Hello. So I am trying to make an Idle game and I'm just experimenting with the progress bar stuff. So what I'm trying to do is make the progress bar autofill to 100% and every time it does that, my population gains by 1. I got the progress bar to work and auto reset but I'm struggling to pull the "population" variable from my "Population" script. I am making the progress bar in the "Progress Bar" script. But I get the error NullReferenceException: Object reference not set to an instance of an object ProgressBar.Start () (at Assets/Scripts/ProgressBar.cs:21) and I haven't found anything during over 30 minutes of search. Here are the scripts below if you guys have any ideas.

Population

  using System.Collections;
   using System.Collections.Generic;
   using UnityEngine;
   
   public class Population : MonoBehaviour {
   
       public UnityEngine.UI.Text populationText;
       public UnityEngine.UI.Text ppc;
       public UnityEngine.UI.Button summon;
   
       public float population = 0f;
       public float populationPerClick;
   
       // Large Number Variables
   
       /*
   
       void Update()
       {
           // Plurals
           // Population Total
   
           if (population == 1)
           {
               populationText.text = population + " human on Earth";
           } else if (population == 0)
           {
               populationText.text = population + " humans on Earth";
           } else if (population > 1)
           {
               populationText.text = population + " humans on Earth";
           }
   
           // Humans Per Click
   
           if (populationPerClick == 1)
           {
               ppc.text = populationPerClick + " human per click";
           }
           else if (populationPerClick == 0)
           {
               ppc.text = populationPerClick + " humans per click";
           }
           else if (populationPerClick > 1)
           {
               ppc.text = populationPerClick + " humans per click";
           }
   
       public void Clicked()
       {
           population = population + populationPerClick;
       }
   }
  

Progress Bar

 using System.Collections; 
 using System.Collections.Generic; 
 using UnityEngine; 
 using UnityEngine.UI;
 
 public class ProgressBar : MonoBehaviour {
 
   public Transform LoadingBar;
   public Transform TextIndicator;
   public GameObject progressBar;
  
   private Population populationScript;
   private float newPopulation;
  
   [SerializeField] private float currentAmount;
   [SerializeField] private float speed;
  
   void Start()
   {
       populationScript = GetComponent<Population>();
       newPopulation = populationScript.population;
  
       Debug.Log(newPopulation);
   }
  
   void Update() 
   {
       if (currentAmount < 100) {
           currentAmount += speed * Time.deltaTime;
           TextIndicator.GetComponent<Text>().text = ((int)currentAmount).ToString() + "%";
  
           newPopulation = newPopulation + 1;
       } 
       else 
       {
           TextIndicator.GetComponent<Text>().text = "Done";
           currentAmount = 0;
  
       }
  
       LoadingBar.GetComponent<Image>().fillAmount = currentAmount / 100;  
   }
 }


So the error occurs on line 21 in the second script (the progress bar script). Anyone got any ideas? Thanks!

alt text

screen-shot-2017-05-27-at-105018-am.png (19.6 kB)
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

2 Replies

  • Sort: 
avatar image
0

Answer by Sackstand · May 28, 2017 at 08:48 AM

make sure that the populationScript is on the same object as your Progress Bar otherwise it will get an NULL back from GetComponent.

Comment
Add comment · 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
0

Answer by FM-Productions · May 27, 2017 at 06:46 PM

simple:

In line 20, populationScript = GetComponent<Population>(); The GetComponent function seems to return null. it cannot find the component you are looking for. You have to attach a Population component to the gameObject where you have attached the ProgressBar script. If you have the population component somewhere else, there are other ways to retrieve it from your scene.

Comment
Add comment · Show 5 · 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 ZachAttack9280 · May 27, 2017 at 07:25 PM -1
Share

When you said, "You have to attach a Population component to the gameObject where you have attached the ProgressBar script,"

Isn't this the right way to do it?

The name of the other script is Population.

public GameObject progressBar; private Population populationScript;

avatar image FM-Productions ZachAttack9280 · May 27, 2017 at 08:10 PM 0
Share

I don't know if you have attached a Population component to your GameObject that also has the ProgressBar component assigned. In the inspector view of your gameObject that has the ProgressBar script attached, you also have to attach the Population script as component for your code to work. It should look like this in the inspector:

Attaching the script as component does not happen in the code, but in the inspector view of the gameObject in Unity itself.

https://images.duckduckgo.com/iu/?u=https%3A%2F%2Fwesrockholz.files.wordpress.com%2F2015%2F12%2Fscreen-shot-2015-12-20-at-9-13-46-pm.png%3Fw%3D1000&f=1

Imagine the Health Pool is your ProgressBar script and the Enemy script would be your Population script. They are on the same gameObject.

avatar image FM-Productions FM-Productions · May 27, 2017 at 08:14 PM 0
Share

Also remember: A NullReferenceException occurs when you try to reference a variable that is still null (trying to access properties or functions on null will result in such an exception). So you can find out why the referenced variable is still null. In your case, GetComponent returns null if the desired component is not attached to your gameObject. If you are unsure how certain functions behave, it is a good strategy to look into the official documentation:

https://docs.unity3d.com/ScriptReference/GameObject.GetComponent.html

Show more comments

Follow this Question

Answers Answers and Comments

322 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 avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image 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

NullReference 2 Answers

NullReferenceException: Object reference not set to an instance of an object 2 Answers

i have written this code to spawn a segments of enemies i don't get errors at all but it didn't work .. what should i do ? 2 Answers

Multiple Cars not working 1 Answer

Object reference is NULL when IT IS set to an instance of an object? 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