I have 2 scripts that need to interact with each other. one script is named MainScript and one is named Flashlight. In my main script i am trying to run a function that is named Reset in my Flashlight script that will reset the flashlight when the function Death is ran in the mainscript. I cant find any methods in doing this.
First you should get a reference to the gameobject that the other script is attached to (if it’s not the same).
Then just use GetComponent. There are also other methods but you’re most-likely looking for that one.
To extend a bit on what @Suddoha mentioned: I would advise you have a quick read and try to understand how components work in Unity. Check out the Manual: Using Components entry. That article will give you a good introduction to what a component is and how it works.
And just so you know why components are relevant to your question, scripts attached to a GameObject in Unity are components. GameObjects are just containers for components. It’s a pretty important concept to grasp early on learning Unity.
I know how to use the game objects and inspector, i dont know the code i shall use to access the variable. I am new to coding so i need help to get the JS script code to get variables from other scripts
using UnityEngine;
using System.Collections;
public class MyScript : MonoBehaviour {
[Header("References")]
public OtherScript otherScript;
void Start()
{
if (otherScript == null)
{
Debug.Log("You forgot to drag otherScript into inspector");
return;
}
Debug.Log(otherScript.varOne);
Debug.Log(otherScript.varTwo);
Debug.Log(otherScript.varThree);
}
}
using UnityEngine;
using System.Collections;
public class OtherScript : MonoBehaviour {
[Header("Settings")]
public int varOne = 5;
public bool varTwo = true;
public string varThree = "Hello";
}
Can you please write it in JS? I dont use C#
It would be a good exercise to to the translation yourself.
https://unity3d.com/learn/tutorials/modules/beginner/scripting/c-sharp-vs-javascript-syntax
If you’re wanting to learn you will want to read the guides that we have listed. If you understand how components work, then you’ll understand how GetComponent, as @Suddoha mentioned, will work. Once you have a reference to your component that has the members you’re wanting access to, you access them using dot notation.
You’ll learn pretty quickly by doing the work, writing the code. People writing the code for you, without explanation especially, isn’t going to be helpful.
Here’s an example to get you started, though. Notice the variable’s type.
using UnityEngine;
public class MainScript : MonoBehaviour {
// A variable that holds a reference to our Flashlight object
// once we assign it in the inspector.
public Flashlight flashlight;
public void Death()
{
// Call Flashlight.Reset on death
flashlight.Reset();
Debug.Log("The Player has died.");
}
}
I cant get the public variable type to work. i created 2 new script and tryed out the public variable and it wont work.
Test1.js:
#pragma strict
public var TestVar1 : float = 50f;
public var TestVar2 : TestVar2;
function Update ()
{
print(TestVar2);
}
Test2.js:
#pragma strict
public var TestVar2 : float = 100f;
public var TestVar1 : TestVar1;
function Update ()
{
print(TestVar1);
}
You have to put an actual type there.
This is the syntax:
var variableName : variableType
In UnityScript, variables are public by default as far as i remember.
You’ve done it correctly with the first variable. The second one will throw an error as neither TestVar1 nor TestVar2 is a type. That’s your variable’s name.
An example for referencing scripts:
Script.js
#pragma strict
var someFloat: float = 50f;
var referenceToSomeOtherScript : AnotherScript;
function Update ()
{
print(TestVar2);
}
If you assign that script to a gameobject, you will be able to drag another object into the inspector slot which has got a script called ‘AnotherScript’ attached.
I am sorry, i cant disect the code or understand your explanation correctly. I can’t understand unity’s explanation of it either. I would learn better if i got the code from someone. I couldn’t find this part in any other script i disected. It would be easier for me if you did use the script names Test1 and Test2 and the variable names TestVar1 and TestVar2.
You just put the name of the script you want to reference behind the colon.
- Create the script called Test1.js:
Test1.js
#pragma strict
var number : float = 50;
var testVar2 : Test2;
function Update ()
{
print(testVar2.phrase);
}
-
Now drag and drop it onto a gameobject.
-
Next, create the script Test2.js and drag it onto a gameobject:
Test2.js
#pragma strict
var phrase : String = "A simple phrase";
var testVar1 : Test1;
function Update ()
{
print(testVar1.number);
}
- Click the gameobject which has Test1.js attached. You see an empty slot in the inspector.
- Drag and drop the gameobject with Test2 into the slot.
The same can be done vice versa, because the other script offers a slot, too.
It’s the same as if you had a variable of type GameObject and put a prefab there.
This is manual assignment via inspector. In order to assign the variables at runtime through code, GetComponent can be used, just as an example.
Thank you, time to mess arround to learn more with this.