Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  2. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

How to reference variables in separate scripts

Discussion in 'Scripting' started by dusthound, Oct 20, 2018.

  1. dusthound

    dusthound

    Joined:
    Sep 18, 2018
    Posts:
    98
    I have a variable in a script attached to a GameObject that I want to reference in a script attached to a prefab. How do I do this? I have tried using GetComponent to reference the script, but that has not worked.
     
  2. newjerseyrunner

    newjerseyrunner

    Joined:
    Jul 20, 2017
    Posts:
    966
    Show your hierarchy because GetConpenent is what would want. You could also make the reference public and drag and drop it.
     
  3. dusthound

    dusthound

    Joined:
    Sep 18, 2018
    Posts:
    98
    You can't just drag and drop an object from a scene to a prefab.
     
  4. dusthound

    dusthound

    Joined:
    Sep 18, 2018
    Posts:
    98
    Actually, i got it to work using GameObject.FindObjectOfType