Accessing a Variable from Another Script C#

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;
}
}

Access a variable from a script in another script

Try making your variable public:

public class MyMissile: ISpellScript
{
public static Vector2 spellpos;

public void CastKeg(ISpellMissile missile)
{
spellpos = new Vector2(daspell.CastInfo.TargetPositionEnd.X, daspell.CastInfo.TargetPositionEnd.Z);
}
}

And now you can access the variable from the other class:

public class GragasQToggle : ISpellScript
{
public void OnSpellCast(ISpell spell)
{
var position = MyMissile.spellpos;
}
}

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.

Passing/Accessing Variable from Another Script c#

Your question is answered here:

https://answers.unity.com/questions/7555/how-do-i-call-a-function-in-another-gameobjects-sc.html

Basically, you create a variable of type "ScriptA" inside script B and put the gameobject containing script A inside the value field of that variable. Then you can access all public functions and variables of script A in script B.

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;
}
}


Related Topics



Leave a reply



Submit