- Home /
How to set a variable equal to another variable in another gameobject?
What I'm trying to do is a build a system where the player can see a weapon on the ground, it clones a new weapon and carries all the set variables over to that clone (ammo etc), pick up the clone, and that should replace the other weapon they're already holding.
My other game object is called Weapon Controller, and that has the script called weaponHandler, which has all the code used to hold any weapon it's given and switch between the two of them. weaponHandler has to two variables, one for each weapon.
Any help I can get I'd appreciate :')
#pragma strict
var weapon : GameObject; //any weapon can be used here
var currentClip : float;
var currentAmmo : float;
var currentWeapon : Component;
function Start ()
{
}
function OnTriggerEnter (other : Collider)
{
print("Item is in range");
var weaponClone : GameObject = Instantiate (weapon); //clone the weapon
currentWeapon = gameObject.GetComponent(WeaponHandler); //check what weapon is in the weapon1 variable
currentWeapon.weapon1 = weaponClone; //change that current weapon to our new cloned weapon
//var currentSelected = currentWeapon.GetComponent(WeaponHandler).weapon1;
//currentSelected = weapon;
}
"How to set" isn't a question. I don't think anyone on this site has ever asked "How DO I", though, so don't worry about it.
Sorry, I guess I meant, this is what I've got so far, hopefully you can see what I'm trying to do, but it doesn't work. So my question would be, what am I doing wrong?
Answer by Loius · Jan 23, 2013 at 08:28 PM
If you're asking the almost-question in the title:
someGameObject.GetComponent(TypeName).variable = aValue;
or
aVariable = someGameObject.GetComponent(TypeName).variable;
GetComponent is "slow", so if you're going to use it a lot it's better to cache the value as a reference:
var typeName : TypeName = someGameObject.GetComponent(TypeName);
typeName.variable = value;
Answer by ChrisHawthorn · Jan 23, 2013 at 10:55 PM
Thanks I tried that out but had no success, I ended up trying something else which I thought would work.
Problem is it now says "An instance of type "WeaponHandler" is required to access non static member 'weapon1'". I changed the weapon1 variable to static, compiled, but of course changes everything, any idea for a solution around this? I'm not sure how to set the appropriate access between it all.
#pragma strict
var weapon : GameObject;
var currentClip : float;
var currentAmmo : float;
var currentWeapon : GameObject;
function Start ()
{
}
function OnTriggerEnter (other : Collider)
{
var weaponClone : GameObject = Instantiate (weapon); //clone the weapon
print("Item is in range");
WeaponHandler.weapon1 = weaponClone;
WeaponHandler.weapon1 = currentWeapon;
Besides it being bad form to submit answers to your own questions, it's not much of an answer if it doesn't work, is it?
I'm just trying to figure it out, it wasn't meant as an answer, just maybe progress that I could use a hand with.
That's cool...but questions with more than one answer tend to get ignored because two is usually sufficient.
Yea that makes sense, I'm still new the forums and the layout is kinda weird to me. I guess I'll just learn my way around. Thanks for the tip though, I won't make that mistake again :')
Your answer
Follow this Question
Related Questions
how can I display a variable as a GUIText 5 Answers
How can I access a variable from another script in a separate object? 1 Answer
Use variable from another script with WWW give NULL? 1 Answer
How to access gameObject variable script 2 Answers
Create a variable on a GameObject for access via the Object without GetComponent() 1 Answer