- Home /
Get Component With Variable Help
So i am trying to set a variable to true in another script using getcomponent but it isnt working and it is giving me an error.
var gunEnabled : String;
var player : Transform;
function OnTriggerStay ()
{
if (Input.GetKeyDown("e"))
player.gameObject.GetComponent("WeaponSelect").gunEnabled = true;
}
Why arent i allowed to do something like this and how can i make it work?
try
player.gameObject.GetComponent<"WeaponSelect">().gunEnabled = true;
cannot infer generic arguements for method UnityEngine.gameObject.GetComponent.() Then it says provide stronger type information through arguements
Seems like i can't print out the greater / less brackets. It is:
player.gameObject*less*YourClass*greater*().gunEnabled = true;
Just look at the second paragraph of the docs: http://docs.unity3d.com/Documentation/ScriptReference/GameObject.GetComponent.html
Answer by cdrandin · Feb 03, 2013 at 02:09 AM
I had this problem before, if I am correct. Do this... player.gameObject.GetComponent(WeaponSelect).gunEnabled = true;
With " " has problems sometimes, so I just never include it
So now when i play it and try it gives me the error $$anonymous$$issing Field Exeption. Field "WeaponSelect.gunEnabled" not found.
does gunEnabled
exist within the script? is it public or internal type? If there are still more issues could you please post the WeaonSelect script.
Ok here it is, the var gunEnabled is a string and i am trying to type the variable name of the gun that is in WeaponSelect script. For example, the gun that is on the walls name is "Beretta" so in the inspector i go to where i can type the string in and i type "berettaEnabled" and that string is one of the variables in the WeaponSelect script that i am trying to set to "enabled".
Answer by melidian · Dec 04, 2014 at 01:22 AM
GameObject.Find("GameObjectsName").GetComponent(WeaponSelect).gunEnabled = true;
Your answer
Follow this Question
Related Questions
How to update variables in a string when displayed? 1 Answer
Accessing other gameobject's script variables : why doesn't this work? 2 Answers
How to access a public string attached on a UI Button 1 Answer
Setting a variable to a variable from another script 1 Answer
Edit not active UI Panel Component 1 Answer