C
C#2y ago
Mosswine

✅ Object reference error? Using Unity

I'm trying to create a slider system to visualize various status bars using references instead of directly setting values in each bar. I can't seem to wrap my head around why this is throwing an 'Object reference not set to an instance of an object' issue. My statblock is here: using System.Collections; using System.Collections.Generic; using UnityEngine; public class FinkratStatblock : MonoBehaviour { public float MaxHealth = 100; public float CurrentHealth = 100; public float MaxEnergy = 200; public float CurrentEnergy = 200; public float MoveSpeed = 7.0f; public float MaxHunger = 100; public float CurrentHunger = 0; public float MaxThirst = 100; public float CurrentThirst = 0; public float MaxSleepy = 500; public float CurrentSleepy = 0; public float MaxCatalystOil = 200; public float CurrentCatalystOil = 200; public float AfraidAmount = 0; } and the healthbar script I'm trying to work on is here. It's saying the error line is line 20, aka the first get component CurrentHealth: using UnityEngine; using UnityEngine.UI; public class Healthbar : MonoBehaviour { public FinkratStatblock finkratStatblock; public float currentHealth; public float maxHealth; public Slider slider; public Image fillImage; private void Awake() { slider = GetComponent<Slider>(); currentHealth = GetComponent<FinkratStatblock>().CurrentHealth; maxHealth = GetComponent<FinkratStatblock>().MaxHealth; } // Start is called before the first frame update void Start() { slider.maxValue = maxHealth; slider.minValue = 0; currentHealth = maxHealth; } void TakeDamage(float amount) { currentHealth -= amount; } // Update is called once per frame void Update() { slider.value = currentHealth / maxHealth; } }
5 Replies
Buddy
Buddy2y ago
Show error Full
Angius
Angius2y ago
Something somewhere is null
Buddy
Buddy2y ago
Avoid calling GetComponent for the same component and object twice According to your log, the error is at line 20 which is where you use GetComponent. Make sure that said component exists on your gameobject! If it does not exist it will return null and give you the error you are getting right now.
Mosswine
Mosswine2y ago
I tried only calling the get component without .currenthealth or .maxhealth but then for some reason it won't let me use those fields as variables. There's a character limit in the original post and I was in kind of a rush to post it before I had to leave for work 😅 I'm specifically doing it apparently the hard way because I want a generic solution i can use for more than just healthbars. As I posted in the statblock, i have several stats I want visualized Further detail now that I have a second to breathe: The character game object does have the statblock attached to it. Ive been able to call the movespeed float in my movement script, so I know Unity knows its there and can access it. Its just in this instance, with the way Im trying to set up my slider, that it isnt working. And the error isnt a compile error, its just unity throwing an exception in the console about the getcomponent as Rust pointed out
Accord
Accord2y ago
Was this issue resolved? If so, run /close - otherwise I will mark this as stale and this post will be archived until there is new activity.
Want results from more Discord servers?
Add your server
More Posts
❔ Display string that divided 1 by 1 word in textbox.I tried to display string sentence 1 by 1 word in textbox. I used string.split() method to split. Bu❔ Will there be issues if the target framework moniker is less than my .NET version installed?Total scrub here, please correct if I say anything wrong. I've got Visual Studio installed with .NET❔ Singleton Pattern StackOverflowExceptionIn the attached code, the GameManager is working fine, but InputManager is throwing a StackOverflowEGet the first letters of a stringHello, Let's say that I have the string "tyranitar". With string[X], I can get the letter that I wa❔ I cant find the Option "ConsoleApp" when creating a new projektI also installed the .NET thing but i cant choose it, one screenshot is from a tut and other one sho❔ hello, Im new in C# codding, could someone help me ? I think something is wrong in my code :(Restoring my source codeHello, I have built a simple C# app, and i debugged it and only got the .config, .exe and .pdb files❔ Callback Url Always NullHere's the two functions involved ``` public IActionResult ExternalLogin(string provider, str❔ Game telemetry & overlay project - help with framework & architecture decisionstl;dr I want to create an application which continuously reads telemetry data from a racing game anderror CS0176: Member 'DateTime.Now' cannot be accessed with an instance reference; qualify it with aHi, i'm a beginner and i come from JavaScript and python. I'm having an issue with my CSharp code: `