- Home /
Get variable in script got by get component c#
I want to create a script for checking which components from an array an object has. The array is in a script called AlleFaehigkeiten and the array's name is array. In this array there is the string "Ruestung", name of a script. This script contains a string Name = "Rüstung". I tried to write this "Rüstung" into the string Testname like this:
for (int i=0; i<GameObject.Find("AlleFaehigkeiten").GetComponent<AlleFaehigkeiten>().array.Length; i++) {
AlleFaehigkeiten Alles = GameObject.Find("AlleFaehigkeiten").GetComponent<AlleFaehigkeiten>();
if(gameObject.GetComponent(Alles.array[i])){
Testname=gameObject.GetComponent(Alles.array[i]).Name;
}
}
This error occured: Type UnityEngine.Component does not contain a definition for 'Name' and no extension method 'Name' of type UnityEngine.Component could be found.
How can I correct this?
using UnityEngine;
using System.Collections;
public class AlleFaehigkeiten : $$anonymous$$onoBehaviour {
public string[] array = {"Ruestung"};
// Use this for initialization
void Start () {
}
// Update is called once per frame
void Update () {
}
}
Don't wonder why it's that short I cut some things which aren't interesting for this.
Answer by maccabbe · Sep 03, 2015 at 02:15 PM
GetComponent(string type) returns an object of type Component. To do what you seem to be doing you would then need to call an explicit conversion. For instance,
var component = gameObject.GetComponent("HingeJoint");
var hingeJoint=component as HingeJoint;
or simply
var hingeJoint = gameObject.GetComponent("HingeJoint") as HingeJoint;
Without a conversion you can only use the component as a component. So while you can use Ruestung.Name
without a conversion you are attempting to call Component.Name
which doesn't exist and why you are getting an error. However you cannot use GetComponent("string") as "string"
so in you case it seems like GetComponent("string")
is pretty much useless.
Consider using if/else of switch statements instead. i.e.
if(Alles.array[i]=="Ruestung")
{
Testname=gameObject.GetComponent<Ruestung>().Name;
}
....
or
switch(Alles.array[i])
{
case "Ruestung":
Testname=gameObject.GetComponent<Ruestung>().name;
break;
...
}
Answer by cjdev · Aug 31, 2015 at 09:22 PM
In your Alles array do you have other strings of different component types besides Ruestung? If so, in your for loop you iterate over the length of the array and your if statement is only checking to see if the other components exist on the gameObject. If they do then you are trying to assign a Name property that doesn't exist in one of them.
Another possibility is that Name isn't defined in Ruestung, though I'm sure you checked that already.
Also a tip on the structure of the coding you have there: when you have a loop like that where you are iterating multiple times you should declare variables that you use in each iteration before-hand to increase performance. GameObject.Find() for example is a particularly expensive method to call so doing it once rather than for each array member can really add up.
There is exactly one element in the array: "Ruestung". The string Name is defined in this Script. I think $$anonymous$$onoDevelop doesn't know which script the array[i] script is and it could not find a string Name in all existing components. $$anonymous$$ay this be right?
And thanks for your tip on my codestructure. I'll use it.
Does the GameObject assigned to the variable named gameObject have a Ruestung component attached to it?
How do you define Testname? The error seems to think Name is a method for some reason.
Your answer
Follow this Question
Related Questions
How to get a component from an object and add it to another? (Copy components at runtime) 12 Answers
Inheritance vs RequireComponent -1 Answers
Many ways to access other components got me very confused. 1 Answer
C# GetComponent, component not turning off. 2 Answers
Need an advise, how to find all objects with a specific name and a specific script? 2 Answers