How can I create a variable in one script and use it in a different script

Hi,
Sorry this might be very simple but I am new to C# and Unity.
I need to get my “dooropening” script to work when my “crate” script collider is over the pressure pad.
I have tried to use a public float but it dose not seem to work.
Any help please ??

Have you googled that? You should get tons of information about that.

You need a reference to the door in order to call methods on it.

The easiest way is probably to expose a ‘door’ variable, i.e. you’ll need a variable of the door script in your pressure pad script. You’ll then drop the door object, which has got the door script on it, into that exposed field in the inspector.

The pressure pad can then access the door that has been linked to it, e.g. call Open() or Close().

Hi Suddoha.
Thanks for the info but I don`t really understand that much about C#.
Can you point me to a example ??

Did you try to google it? Or search the forums. Or Unity tutorials? This question has been asked so many times on the forums alone in so many different ways, you should have no trouble finding the example yourself.

https://unity3d.com/learn/tutorials/topics/scripting/getcomponent

You really should take the time out to do all the scripting tutorials.

yeah, i literally just had this problem and then i googled it. use getcomponent to get a reference to the script then you can use that to access the variable if it is public.

the example of what i just did if the script you’re calling from is attached to the same game object as the script you are trying to get the variable from:

classname objectname;

Start()
{
classname objectname = gameObject.GetComponent<classname>();

}

Update()
{
if (objectname.variablename == 1)
    do stuff;
}

if the script is attached to a different game object - just look up getcomponent and it should tell you.

Hey Thanks very much for the help. I`m not too sure with the get component but I will look into now.

Hello! The solution is easy to implement. Let’s say you have: two scripts: a dooropening and a crate and you want to use a variable declared into the crate script in the dooropening.

So the crate script would be sth like this:

using UnityEngine;

public class Crate : MonoBehaviour {

public float myFloat = 0.1f;

}

And the dooropening script:

using UnityEngine;

public class DoorOpening : MonoBehaviour {

public Crate crate;
public float doorOpening = crate.myFloat;

}

Then you should go in the inspector in the DoorOpening script and in the empty field ‘Crate’ attach the game object that have the crate script as a component.

Hope that I could help you.

Alex

1 Like

Hi Alex.
Really sorry for this late reply , I thought I had sent this a few days ago but it did not send ??
I have been playing with the GetComponent - Unity Learn tutorial but still getting errors.

your idea looks really simple so I have made a simple test project as below , but still a get an error with the crate (in red below)??

" Assets/Door.cs(9,31): error CS1061: Type Crate' does not contain a definition for myFloat’ and no extension method myFloat' of type Crate’ could be found. Are you missing an assembly reference? "

Any ideas would be very welcome…I will keep looking

Thanks
Tony

***************This is attached to the Crate object ***********************
using UnityEngine;

public class Crate : MonoBehaviour {

public float myfloat = 0.1f;

// Use this for initialization
void Start () { }


******************* This is attached to the door object **************************
using UnityEngine;

public class Door : MonoBehaviour {

public Crate crate;

public float door = crate.myFloat;

// Use this for initialization
void Start () { }

This Script

public class Door : MonoBehaviour {

    public Crate crate;

    public float door = crate.myFloat;

Should be this

public class Door : MonoBehaviour {

    public Crate crate;

    public float door = 0;

void Start()
{
door = crate.myFloat;
}

You cannot assign variables from other scripts when declaring new variables, they must be assigned in the start loop (if only changing once) or in the update loop (if changing constantly)

1 Like

Hi.
Thanks for that Rob, I will try it tonight.

GREAT !!
Thank everyone that is now working. onto the next problem now !!