Trying to set a variable from 1 script to another script.. Doesn't work - Please help.

Hi Everyone,

TurretBuilder Script have to choose which turret we have to build,
I set the “Turret” that belong to “TurretBuilder” - in the console.
When we press on the Picture of turret (GameObject) ,

And
ToBuild script , is actually build the turret choosen, which set before in TurretBuilder script…

Actually , I got this error:
UnassignedReferenceException: The variable TTB of ToBuild has not been assigned.

There is the scripts:

Please help

ToBuild Script:

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class ToBuild : MonoBehaviour
{
    public Transform TTB;


    public void GetTurretToBuild(Transform _TTB)
    {
        TTB = _TTB;
    }
    void OnMouseDown ()
    {
        Instantiate(TTB, transform.position, transform.rotation);
        Debug.Log("We Instantiated object");
    }

}

TurretBuilder Script:

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class TurretBuilder : MonoBehaviour
{

    public Transform turretGO;
    public Transform turretSet;


    void Update ()
    {
         turretSet = turretGO;
    }

    void OnMouseDown ()
    {
       
        Debug.Log("We Click TuerrtBuilder");
        GameObject placer = GameObject.Find("ToBuild");
        placer.GetComponent<ToBuild>().GetTurretToBuild(turretSet);





    }
       
}

You say you set “Turret” but I only see “turretGO” and “turretSet”

Have you assigned anything to “turretGO”?

1 Like

Yeah you right…
I

Do not know if you still need help but, how I do it is I make a public variable named after the script you wish to access. Then you can call variables from that script.

Example:

public ScriptTittle myscriptvar;

if (myscriptvar.VariableIWantToSelect == 1)
{
Debug.Log(“Test”);
}

The problem was silly,

I didnt set the value in while running the program.

Anyway, Thanks :slight_smile: