Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 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 SmooveB · Jan 26, 2014 at 05:42 PM · variablescomponentgetarbitrary

Getting Variables From An Arbitrary Component?

I am familiar with the basic way of getting components by

 ASpecificComponent aSpecificComponent = (ASpecificComponent)transform.GetComponent("ASpecificComponent");
 
 aSpecificComponent.anInt = 0;


However, now I have an array of components and I want to do something like:

 compnentArray[1].anInt = 0;

or

 Component aComponent = compnentArray[1];
 
 aComponent.anInt = 0;

Trying to do that yields this error: Assets/Scripts/CharController.cs(111,38): error CS1061: Type UnityEngine.Component' does not contain a definition for anInt' and no extension method anInt' of type UnityEngine.Component' could be found (are you missing a using directive or an assembly reference?)

I suspect that it is not possible to do what I want to do but that there is something else that you do in a situation like this. By the way, all of the scripts in the array of components have a variable named anInt.

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 Owen-Reynolds · Jan 26, 2014 at 06:06 PM 1
Share

Does anyone know javascript? The problem is that the array is declared as just components, when it should be an array of whatever the script name is.

avatar image robertbu · Jan 26, 2014 at 06:16 PM 1
Share

You don't show us the array declaration, so we are left to guess a bit on how you have things setup. If all the entries in the array are different instances of the same script, then @Owen Reynolds is right. You need to declare an array of the specific script. If the array is from different scripts, then you need to create a base class with 'anInt' and then derive all your other scripts from this base class. The array use the base class.

avatar image SmooveB · Jan 26, 2014 at 07:04 PM 0
Share

The array declaration is like this public Component[] componentArray; and is filled by drag and drop. So your second supposition is correct, the array is from different scripts.

So let me see if I understand. I can create a class anywhere as

public class BaseScriptTyoe : $$anonymous$$onoBehaviour

that includes the variable. Then I can call the main class of a particular script

public class AParticularScripOne : BaseScriptType

Then I can create an array like

 `public BaseScriptType[] componentArray;` 


and it will take all of my particular scripts because of polymorphism?

0 Replies

· Add your reply
  • Sort: 

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

19 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

Related Questions

Structuring view of my script/component in Inspector 0 Answers

Why does my variable show a rising score AND 0 at the same time? 2 Answers

Adding variables to the script components 1 Answer

Find all gameObjects with same tag 1 Answer

Object Reference Required To Access A Non-Static Field 1 Answer


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