GetComponent returns null (154540)

Hi all,

I am receiving null when I perform a GetComponent(). I saw that this problem is quite frequently asked on this forum, in these cases the problem is mostly caused by:

  • The script not being attached to the gameobject
  • The instance variable not being initialized

I don’t think this is the cause here though - or I am missing something. As following my code (note that I am new to Unity). In the console log I can see the following output:

Start
 PlayerBall is null
 End

Meaning the LineRenderer linked to the gameobject can be resolved, but the Player object can not.

Player class:

public class Cue : MonoBehaviour {
    public GameController game;
    public LineRenderer line;
    public Player playerBall;

    Cue()
    {

    }
	void Start () {
        Debug.Log("Start");
        line = GetComponent<Cue>().line;
        if (line == null)
        {
            Debug.Log("Line is null");
        }
        playerBall = GetComponent<Player>();
        if (playerBall == null)
        {
            Debug.Log("PlayerBall is null");
        }
        Debug.Log("End");
    }

    // Update is called once per frame
    void FixedUpdate()
    {
        /* do stuff */
    }

    void OnMouseDown()
    {
        
    }
}

In Unity, the following properties are set:
59556-capture.png

The Player object:

public class Player : MonoBehaviour
{
    private int count;
    public Text UIScore;
    public Rigidbody rb;

    protected void Start()
    {
        rb = GetComponent<Rigidbody>();
        count = 0;
        updateScore();
    }
    void FixedUpdate()
    {
        /* do stuff*/
    }

    void OnMouseDown()
    {
    /* do stuff */
    }

    void OnCollisionEnter(Collision collision)
    {
        if (collision.gameObject.CompareTag("Ball"))
        {
            count++;
        }
        updateScore();
    }

    void updateScore()
    {
        UIScore.text = "Player ball Collision count: " + count.ToString();
    }
}

Its better but your problem isn't really there. I think the problem is in the way you draw the lines, because shouldn't "Stats Summury" come from "Stats" instead of "Robot" ? If that's right then "Robot" should have only one child and the line should stop at point where it goes to "Build Robot" Maybe show your code ( the real one not some pseudo code) from drawConnectingLines() ? It would be way easier to spot what is wrong.

–

1 Answer

1

you wrote a dirty code. In Cue class you have line object(LineRenderer) so you don’t need to use
“line = GetComponent().line;” and also
“playerBall = GetComponent()”;

Oh dang, I got it. Yes you are right - stats summary and build robot are on the same level(indentation) and what I do is I draw from the last to the first tile on the level, which doesnt take into account if the tiles have different parent. I guess I should draw the line not to the first panel on the level but to the first panel with the same parent on the level. Thanks, well see if that works.

–