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 focus915 · Nov 17, 2014 at 07:23 AM · gameobjectobjectclassaddcomponent

add class or script to child gameObjects

Hello all,

I am attempting to make a new object of a class, assigning values to class variables then attaching that object to a gameObject via a editor button press. I'm currently calling the following code when a button is pressed in the editor:

 class nodeList{
     public float distance;
     public nodeList(float x){
        distance = x;
     } }
 
 foreach(Transform child in transform){
      nodeList temp = new nodeList(dist);
      child.gameObject.AddComponent<temp>(); }

this throws an error: 'temp' could not be found. Are you missing a directive or assembly reference?

I also tried creating a new empty script called "nodeList" using: child.gameObject.AddComponent("nodeList") which throws klass != SCRIPTING_NULL

I also tried child.gameObject.AddComponent(nodeList) as Component; which throws Only assignment, call, increment, decrement and new object expressions can be used as a statement.

Can anyone tell me what I am doing wrong?

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 NaRango4 · Nov 18, 2014 at 12:30 PM 0
Share

you cannot loop in transform directly, you need to do as below

 for(int i=0; i<transform.childCount;i++)
 {
 transform.GetChild[i].gameObject.AddComponent<proxy>();
 }
avatar image focus915 · Nov 18, 2014 at 05:11 PM 0
Share

I don't think the foreach loop is at fault. I can perform other logic on the children gameObjects perfectly fine. I'll give it a shot though.

avatar image focus915 · Nov 18, 2014 at 05:18 PM 0
Share

Nope. Get the same error, 'scriptname' could not be found. Are you missing a directive or assembly reference?, also your code should be transform.GetChild(i) as it is a function call.

1 Reply

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

Answer by tanoshimi · Nov 17, 2014 at 07:24 AM

You can't attach arbitrary classes such as your nodeList to gameobjects. To assign a component to a gameobject, the class must derive from Monobehaviour.

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 focus915 · Nov 17, 2014 at 07:21 PM 0
Share

I see. Why would attacing a new script that drives from $$anonymous$$onoBehaviour throw and error and fail? child.gameObject.AddComponent("nodeList") which throws klass != SCRIPTING_NULL where nodeList is a new $$anonymous$$onoBehaviour script.

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

3 People are following this question.

avatar image avatar image avatar image

Related Questions

Instantiating gameObject with custom Class properties 1 Answer

How to remove objects from a list ? 3 Answers

Problem creating a script to destroy object on collision. 1 Answer

Transform.localPosition Script Problem. 3 Answers

Assets/SkyAir.cs(13,25): error CS0029: Cannot implicitly convert type `UnityEngine.GameObject[]' to `UnityEngine.Object' 3 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