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 Cuxxl · Nov 30, 2020 at 07:20 PM · c#unity 2derror messagereference

Referencing a script from another script does not work,Referencing functions from another Script isnt working

I am getting more and more frustrated and just can't get to the bottom of this problem I have.I want to call a function from another Script in my "Player"-Script but Unity keeps throwing this error: NullReferenceException: Object reference not set to an instance of an object Player.Start () (at Assets/Scripts/Player.cs:22) I have looked up multiple solutions but none of them seem to work. What is confusing me, is the fact that the functions and the class itself in the "PowerBar"-Script show that they are being referenced in the "Player"-Script. I don't get why Unity tells me that they are not referenced to.


This is the Player-Script: (PlayerMovement is the CharacterController-Script, which works perfectly fine)

 using System.Collections;
 using System.Collections.Generic;
 using UnityEngine;
 
 public class Player : MonoBehaviour
 {
     public PlayerMovement playerMovement;
     float horMove = 0f;
     bool jump = false;
     bool crouch = false;
     public float runSpeed = 40f;
 
     public PowerBar powerBar;
     public int minPower = 10;
     public int currentPower;
 
 
     // Start is called before the first frame update
     void Start()
     {
         currentPower = minPower;
         powerBar.SetMinPower(minPower);
     }
 
     // Update is called once per frame
     void Update()
     {
         PlayerInput();
         GainPower(10);
     }
 
     void FixedUpdate()
     {
         // Move Character
         playerMovement.Move(horMove * Time.fixedDeltaTime, crouch, jump);
         jump = false;
     }
 
     void PlayerInput()
     {
         horMove = Input.GetAxisRaw("Horizontal") * runSpeed;
 
         if(Input.GetButtonDown("Jump"))
         {
             jump = true;
            }
 
         if(Input.GetButtonDown("Crouch"))
         {
                 crouch = true;
         } else if(Input.GetButtonUp("Crouch"))
            {
               crouch = false;
            }
     }
 
     void GainPower(int gain)
     {
         if(Input.GetKeyDown(KeyCode.Y)) 
         {
             currentPower += gain;
             powerBar.SetPower(currentPower);
         }
     }
 }



This is the PowerBar Script:

 using System.Collections;
 using System.Collections.Generic;
 using UnityEngine;
 using UnityEngine.UI;
 
 public class PowerBar : MonoBehaviour
 {
     public Slider slider;
     public Gradient gradient;
     public Image fill;
 
     public void SetMinPower(int power)
     {
         slider.minValue = power;
         slider.value = power;
 
         fill.color = gradient.Evaluate(1f);
     }
     public void SetPower(int power)
     {
         slider.value = power;
 
         fill.color = gradient.Evaluate(slider.normalizedValue);
     }
 }

I hope someone can help me because I spent way too much time on this simple problem. Both of the Scripts are stored in the same folder by the way, so that can't be the problem.

Comment
Add comment · Show 1
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 gjf · Nov 30, 2020 at 07:19 PM 0
Share

Did you assign the power bar in the inspector? That's most likely why you're seeing this issue.

1 Reply

· Add your reply
  • Sort: 
avatar image
1
Best Answer

Answer by sacredgeometry · Nov 30, 2020 at 07:27 PM

You aren't initialising powerBar anywhere before the Start method is run. So it's null. Which is why you are getting a null reference exception.

Comment
Add comment · Show 2 · 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 Cuxxl · Nov 30, 2020 at 07:48 PM 0
Share

And how do I initialize powerBar? I am just getting started with Unity so I don't know very much about it. And the Player$$anonymous$$ovement script isn't initialized either or am I missing something obvious? I thought I just had to reference the other script and I could start.

avatar image Cuxxl · Nov 30, 2020 at 07:52 PM 1
Share

Of course I was just missing the Power Bar on the Player Script! Cant believe that tookme so long. Thank you!

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

159 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

Related Questions

Multiple Cars not working 1 Answer

Distribute terrain in zones 3 Answers

After instantianting gameobjects, unity cant asign references anymore 1 Answer

csc.exe has stopped working ,csc.exe has stoped working 0 Answers

Prefab, referencing them, using their methods:bug or intentional? 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