Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by melwei · Aug 31, 2015 at 06:33 PM · c#arraygetcomponentcomponent

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?

Comment
Add comment · Show 3
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image whaleinthesea · Aug 31, 2015 at 06:38 PM 0
Share

Can you include your AlleFaehigkeiten script?

avatar image melwei · Sep 01, 2015 at 01:41 PM 0
Share
 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 () {
     
     }
 }
 
avatar image melwei · Sep 01, 2015 at 01:42 PM 0
Share

Don't wonder why it's that short I cut some things which aren't interesting for this.

2 Replies

· Add your reply
  • Sort: 
avatar image
1
Best Answer

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;
       ...
 }

Comment
Add comment · Show 1 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image melwei · Sep 04, 2015 at 12:52 PM 0
Share

Looks like it could work. Have to try it out...

avatar image
0

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.

Comment
Add comment · Show 5 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image melwei · Sep 01, 2015 at 01:39 PM 0
Share

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.

avatar image cjdev · Sep 01, 2015 at 07:01 PM 0
Share

Does the GameObject assigned to the variable named gameObject have a Ruestung component attached to it?

avatar image melwei · Sep 02, 2015 at 05:25 PM 0
Share

Yes it has.

avatar image cjdev · Sep 02, 2015 at 07:10 PM 0
Share

How do you define Testname? The error seems to think Name is a method for some reason.

avatar image melwei · Sep 03, 2015 at 11:05 AM 0
Share
   public string Testname="Test"

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

31 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

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


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges