- Home /
GIVING variables to another gameobject
HAI! I have two gameobjects, for example, powerup and player. Player and powerup have the same variables, for example:
var health : float;
var shield : bool;
var ammo : int;
I'm looking for a script, that, when player x,z position (y does not matter, 2d game) is the same as powerup x,z position, copies variables values FROM powerup, and gives it TO player. The script should be attached to powerup. I thought about something like reverse GetComponent (GiveComponent?).
Waiting for your reply, Kisiel
Answer by xortrox · Apr 21, 2014 at 02:09 PM
This would require the "OtherObject" having a "OtherComponent" script attached that also has a variable named shared that is public.
One example:
public class ObjectComponent : MonoBehaviour
{
public int shared = 10;
void Awake()
{
GameObject.Find("OtherObject").GetComponent<OtherComponent>().shared = this.shared;
}
}
That is correct, in javascript you just do
function Awake()
and
public var shared : int = 10;
I think.
You need to specify which component to get from Cube
GetComponent<SomeComponentType>()
public class CubeComponent : $$anonymous$$onoBehaviour
{
public bool allowleft = false;
}
Answer by $$anonymous$$ · Apr 21, 2014 at 04:08 PM
you could put:
var sharedvalue : int;
function Update() { sharedvalue = otherscriptname.varname; }