- Home /
GetComponent Variable Referenced Twice
I'm making a fighting game that uses three main scripts per character. There's the stats, control, and projectile scripts. The stats and control both go within the character while the projectile script is in my projectile prefab. A variable (Character2) was GetComponent-ed from the stats script to the control script. It worked. When I did the same with the projectile script, it wouldn't set and stayed as zero.
Can a variable only be referenced once to before the GetComponent action is unusable? I don't want to combine the Stats and Control scripts, but I will if I need to.
UPDATE: All right. I'll try out what Tehnique is suggesting, but first, I'll paste some of my script here.
This is for Stats2:
var Character2 = 15;
Control2 (receiving the variable works):
var Character2 : int;
function Update () {
//Receive character variables.
Character2 = GetComponent("Stats2").Character2;
Projectile2 (receiving the variable does not work):
var Character2 : int;
function Update () {
//Receive character variables.
Character2 = GetComponent("Stats2").Character2;
GetComponent() returns a reference to a script - it should never return zero. Can you please post a section of the relevant code you are using.
I think you mean "null", not zero. Anyway, if you copy-pasted the code from control script to projectile script, it won't work. That's because in Control you can use transform.gameObject.GetComponent to access the stats script (they share the same transform). The projectile script sits on a projectile from what I understand, so you need to refer the character in the projectile script and try to get the stats component from the character.
Short version: make sure you run "GetComponent" on the correct gameObject (character in your case).
Are you sure that works? I've made projects before when I could use GetComponent between two different gameObjects at different positions. I'm using just plain GetComponent, not transform.gameObject.GetComponent.
Answer by Tehnique · Jul 13, 2014 at 08:11 PM
As stated in the comments, your problem is that you are using "GetComponent" which only looks on the current object for the component. You should use a reference to the object you want to find the component on and call "referencedObject.GetComponent".
I'm not sure how you got it to find components on other objects without referencing that object and using just "GetComponent", but it shouldn't. For more information look here.
Also, this read might help. Best thing to do is only call "GetComponent" in start and keep a private reference to the found component, so you don't have to call "GetComponent" multiple times in Update.
Your answer
Follow this Question
Related Questions
How to make camera position relative to a specific target. 1 Answer
Passing a Script Name to a Function 2 Answers
How to set a variable equal to another variable in another gameobject? 2 Answers
Why can't I change another script's variable with this script? Thanks, 1 Answer
AddComponent with parameter variable 2 Answers