Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.

[Question] Help, I can't access to a method from a another class..

Discussion in 'Getting Started' started by Gr00vy_, Aug 5, 2022.

  1. Gr00vy_

    Gr00vy_

    Joined:
    Aug 2, 2022
    Posts:
    10
    Hello Everybody,

    Sorry for my english, I'm french but french unity forums are generally dead ( or less active ).

    I have a big problem who is the following :

    I've two Objects : a ball object and a bar object who have each their respective script.

    Here is my ball.cs class :

    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4.  
    5. public class ball : MonoBehaviour
    6. {
    7.  
    8.     public float mVelocity = 0.6f;
    9.  
    10.     public Rigidbody2D rb;
    11.  
    12.     // Start is called before the first frame update
    13.     void Start()
    14.     {
    15.         rb = GetComponent<Rigidbody2D>();
    16.         rb.velocity = new Vector2(Random.Range(0.5f, 0.8f)*mVelocity, Random.Range(0.5f, 0.8f)*mVelocity);
    17.     }
    18.  
    19.     // Update is called once per frame
    20.     void Update()
    21.     {
    22.         rb.velocity = mVelocity * rb.velocity.normalized;
    23.     }
    24.  
    25.     public Vector2 getCenterBall()
    26.     {
    27.         return rb.centerOfMass;
    28.     }
    29. }
    30.  
    Here is my bar.cs class :

    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4.  
    5. public class bar : MonoBehaviour
    6. {
    7.  
    8.     private float mVelocity = 10f;
    9.     public ball b;
    10.  
    11.     // Start is called before the first frame update
    12.     void Start()
    13.     {
    14.         b = GetComponent<ball>();
    15.     }
    16.  
    17.     // Update is called once per frame
    18.     void Update()
    19.     {
    20.          MouseBarRotation();
    21.      }
    22.  
    23.     void MouseBarRotation()
    24.     {
    25.         Vector2 test = b.getCenterBall();
    26.         print(test.x);
    27.     }
    28.  
    29. }
    30.  
    As you can see, in my bar.cs class I try to call the method getCenterBall() of my ball that I've instancied with the name "b" at the beginning but when I launch my program, I have the following error :

    Code (CSharp):
    1. NullReferenceException: Object reference not set to an instance of an object
    2. bar.MouseBarRotation () (at Assets/Scripts/bar.cs:25)
    3. bar.Update () (at Assets/Scripts/bar.cs:20)
    This error pointed the following line of my bar.cs class :

    Code (CSharp):
    1.         Vector2 test = b.getCenterBall();
    I joined a screenshot to this thread to show you my project arborescence.

    I lost 2 hours with full google and youtube searching and all solutions I found didn't work.. It make me crazy :mad:

    Thanks a lot for my future lifesaver :)
     

    Attached Files: