How to access a variable from another script in another gameobject through GetComponent?
You need to find the GameObject that contains the script Component that you plan to get a reference to. Make sure the GameObject is already in the scene, or Find
will return null
.
GameObject g = GameObject.Find("GameObject Name");
Then you can grab the script:
BombDrop bScript = g.GetComponent<BombDrop>();
Then you can access the variables and functions of the Script.
bScript.foo();
I just realized that I answered a very similar question the other day, check here:
Don't know how to get enemy's health
I'll expand a bit on your question since I already answered it.
What your code is doing is saying "Look within my GameObject for a BombDropScript
, most of the time the script won't be attached to the same GameObject.
Also use a setter and getter for maxBombs
.
public class BombDrop : MonoBehaviour
{
public void setMaxBombs(int amount)
{
maxBombs += amount;
}
public int getMaxBombs()
{
return maxBombs;
}
}
Accessing the variable from another Script on the same GameObject
idkhorsey ih = go.GetComponent<idkhorsey>();
can not be done in a static
context at field declaration.
Rather do it e.g. in
private idkhorsey ih;
private void Awake()
// or
//private void Start()
{
ih = go.GetComponent<idkhorsey>();
}
Or if you say it is on the same GameObject
anyway then why even use go
? Simply use
private idkhorsey ih;
private void Awake()
// or
//private void Start()
{
ih = GetComponent<idkhorsey>();
}
However, way easier would be directly using
public idkhorsey ih;
//or
//[SerializeField] private idkhorsey ih;
and drag your according object into that field directly and forget about go
.
Access component from another script
If they are on the same object than
myScript = GetComponent<Outline>();
should already give you the reference you want.
Otherwise if you say it is on the parent object than you should instead use
myScript = transform.parent.GetComponent<Outline>();
or GetComponentInParent (only if the component is enabled and the GameObject active on Start)
myScript = GetComponentInParent<Outline>();
Even better (if possible) would be you make it a [SerializeField]
[SerializeField] private Outline myScript;
and directly reference it via the Inspector than you don't have to use GetComponent
at all. Drag&Drop the according GameObject
in the field it automatically gets the according component reference.
In order to then enable or disable it simply set MonoBehaviour.enabled
myScript.enabled = true; // or false
Use variable from another script or define it?
TLDR; Getcomponent()
calls are expensive. It is OK if used once/rarely, but starts to have an effect when called continiously.
Store a reference (define) to it:
public class GetController : MonoBehaviour
{
// Will show up in unity's inspector.
// Drag-and-Drop the gameObject with the 'Controller' component attached to it.
[SerializeField]
private Controller yourController ;
void Dosomthing(){
var Ma = yourController.Manager
}
}
Also, most things in Unity can be exposed to the Inspector as long as it is serializable.
You can expose a Controller
field to the inspector, and drag-drop the same GameObject.
This lets you skip a GetComponent
call and another define.
Access variables/functions from another Component
How to access variables/functions from another Class. The variable or function you want to access or called must be public
not private
.
public class ScriptA : MonoBehaviour{
public int playerScore = 0;
void Start()
{
}
public void doSomething()
{
}
}
Access variable playerScore
in ScriptA
from ScriptB
. First, find the GameObject that the script or component is attached to with the GameObject.Find
function then use the GetComponent
function to retrieve that script or component that is attached to it.
public class ScriptB : MonoBehaviour{
ScriptA scriptInstance = null;
void Start()
{
GameObject tempObj = GameObject.Find("NameOfGameObjectScriptAIsAttachedTo");
scriptInstance = tempObj.GetComponent<ScriptA>();
//Access playerScore variable from ScriptA
scriptInstance.playerScore = 5;
//Call doSomething() function from ScriptA
scriptInstance.doSomething();
}
}
How can I access a variable from another script in Unity?
Try this:
public class Particle : MonoBehaviour
{
public static Particle instance;
public float particleSize;
public Transform particle;
void Awake()
{
instance = this;
}
void Start()
{
particle.localScale *= particleSize;
}
}
public class Magnetic : MonoBehaviour
{
public Transform magnetic;
void Start()
{
magnetic.localscale *= Particle.instance.particleSize;
}
}
Related Topics
Mono High Resolution Timer (On Linux)
How to Check If a Type Is a Subtype or the Type of an Object
Update Row If It Exists Else Insert Logic with Entity Framework
How to Get a List of Users from Active Directory
How to Compare 2 Files Fast Using .Net
How to Put Delay Before Doing an Operation in Wpf
Selectsinglenode Returns Null When Tag Contains Xmlnamespace
Select Right Generic Method with Reflection
Unauthorizedaccessexception Cannot Resolve Directory.Getfiles Failure
Callback When Dependency Property Recieves Xaml Change
How to Get JSON.Net to Serialize Members of a Class Deriving from List<T>
C# Equivalent of SQL Server Datatypes
Difference Between Invariantculture and Ordinal String Comparison
Using C# to Check If String Contains a String in String Array
Reference Assemblies for Framework ".Netframework,Version=V4.7.1" Were Not Found