Alright, so I kind of understand the basics of GetComponent and GameObject.Find, but I can’t seem to find out how to access a variable from one object in another object.
I don’t want to make the variable global via “static var” because I’ll have multiple instances of each object.
For instance, let’s say I have MyVariable1 in an instance of Object A and MyVariable2 in an instance of ObjectB. When the two collide, I want to use both of the variables for something. How can I access the variable from one object in the other?
I’m probably either missing something or just plain doing things wrong, because I’m still a Unity newb.
if you have a script called ‘foo’ attached to gameObject ‘b’, and you want to access the variable ‘thing’ with a different script attached to gameObject ‘a’, there are a couple choices:
If you want to find it but not change it, use a.GetComponent(foo).thing
if you want to change the variable, make a function in the script ‘foo’ like this:
function Var (somthing) {
thing = something;
}
then, in the other script, do thisa.SendMessage ( "Var", someValue);
Hmmmm…but how do I get a? I’m in a script in object b, so if I type “a.GetComponent(foo).thing” it’s not going to know what “a” is. I have to find the object and the variable together, right?
While I wait for an answer I’ll try your second option and the other options in this thread.
Basically, I want to calculate the damage a character receives based on the strength of the attacking opponent and the defense of the one getting hit. In the opponent’s attack script is the “power” variable (how strong that specifc attack is). That’s what I want to retrieve.
EDIT: Oh wait, I think that line worked! I just forgot to assign it to a variable. doh
Probably the problem is GetComponent returns a Component object, not an AttackTemplate object. You need to say to the compiler to treat that component as an AttackTemplate script.
(GetComponent(“AttackTemplate”) as AttackTemplate).power
Also to get a component you can use GetComponent(typeof(AttackTemplate)) or GetComponent() in C#. Both are more faster than passing a string to the method.
Alright, so basically what I’m working on is a sort of multiplayer fighting game (not networked, on a single machine). I have very little done yet, but that’s because I don’t want to work on things like controls or assets until I actually get the basics working.
Right now I have a very naive and overly simplistic damage formula as a placeholder.
Here is the “Character Template” script. It will be attached to any player object. The text in quotes is pseudocode (well, apart from tags), as I haven’t been able to reference the other object’s variables yet:
var health = 10;
var MaxHealth = 10;
var attack = 2;
var defense = 1;
var damage;
function OnTriggerEnter (other : Collider)
{
if (other.gameObject.CompareTag ("Attack"))
{
damage = "((the attacks's power variable + the agressing player's attack variable) / this object's defense)";
if (health - damage > 0)
{
health -=damage;
}
else
{
health = 0;
}
}
}
And this is the “Attack Template” script that will be applied to any attack object.
var power = 1;
var IsProjectile = false;
function OnTriggerEnter (other : Collider)
{
if (other.gameObject.CompareTag ("Player"))
{
if (IsProjectile)
{
Destroy (gameObject);
}
}
}
The IsProjectile variable is so I can use the template for both melee and ranged attacks.
Yes, I realize there isn’t much done there at all.
EDIT: I also just realized a major flaw in how I’m trying to do it. If I try to access a variable from the script directly, then it won’t be working from the actual object’s copy, right? It will be working from the script’s defaults, not that of the individual instances. I need to work from the actual instance colliding with the Player. I realize this is probably all much simpler than I’m making it.
Thats how you get variables out of the objects script.
var health = 10;
var MaxHealth = 10;
var attack = 2;
var defense = 1;
var damage : int;
function OnTriggerEnter (other : Collider)
{
if (other.gameObject.CompareTag ("Attack"))
{
damage = other.GetComponent(AttackTemplate).power - defense;
if (health - damage > 0)
{
health -=damage;
}
else
{
health = 0;
}
}
}
I am actually still confused when looking at this. I typed the other.GetComponent() but it said it didn’t exist in the context. Is there a certain namespace I must use fore this?