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.
  2. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice

Question Help With Public Variable Access Using Get Component

Discussion in 'Getting Started' started by AndyZuntz, Apr 28, 2021.

  1. AndyZuntz

    AndyZuntz

    Joined:
    Mar 12, 2021
    Posts:
    5
    Hi Everyone.
    I hope you are all ok.
    Newbie to Unity here, going mad looking at sharing variables between scripts.
    I have done loads of googling and reading and I must be making a very basic mistake somewhere.

    I simply want, as a learning exercise, to create a string as public in one script and print it to the console in another script.

    I have spent ages online - into my third day, so please put me out of my misery.

    To keep it really simply I took the example from the unity.learn area:

    https://learn.unity.com/tutorial/getcomponent#

    and made my own mini version.

    And no matter how much I tidy it up and look for mistakes I always get the error

    NullReferenceException: Object reference not set to an instance of an object
    ScriptNo2.Start () (at Assets/Scripts/ScriptNo2.cs:15)


    If anyone can help me I will be eternally grateful.

    Andy

    ===

    So here are the two scripts:

    First the one to create the string:

    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4. public class ScriptNo1 : MonoBehaviour
    5. {
    6.     public string outText = "Bernie Sanders";
    7. }
    and second the one to access that variable and print it to console:

    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4.  
    5. public class ScriptNo2 : MonoBehaviour
    6. {
    7.     private ScriptNo1 scriptNo1;
    8.  
    9.     void Awake()
    10.     {
    11.         scriptNo1 = GetComponent<ScriptNo1>();
    12.     }
    13.     void Start()
    14.     {
    15.         Debug.Log("A message from Start in Script 2 drawing variable from Script 1 " + scriptNo1.outText);
    16.     }
    17. }
     
  2. Lurking-Ninja

    Lurking-Ninja

    Joined:
    Jan 20, 2015
    Posts:
    10,036
    Make sure you put both scripts on the same game object. The GetComponent<>() method is running on the current game object, which means it only finds components on it.
    Here I put them on my Main Camera for test:
    screenshot1.png
     
    AndyZuntz likes this.
  3. AndyZuntz

    AndyZuntz

    Joined:
    Mar 12, 2021
    Posts:
    5
    Wow. You are very quick to reply. But I guess that is what a lurking Ninja does!

    Thank you so much for your help. It works perfectly now and I can move on !

    And as I said, I am eternally grateful!

    Andy
     
    Joe-Censored and Schneider21 like this.