• 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
1
Question by Mobilpadde · Oct 29, 2014 at 04:17 PM · addcomponent

AddComponent can't seem to find any of my scripts

I'm having some troubles with AddComponent as every time I try to use it, it doesn't seem to work, or be able to find the script that I'm trying to add to a gameobject. I have no idea why this isn't working, thus I'm seeking advice with you guys.

Screendump: http://i.imgur.com/iTuctLk.png

Part of my code that isn't working (Plus a couple of lines that is):

 GameObject player = GameObject.CreatePrimitive (PrimitiveType.Cube);
 player.transform.position = new Vector3 (0, worldHeight, -0.1f);
 player.renderer.material.color = Color.red;
 player.name = "Player";
 player.AddComponent ("Player");
 
 GameObject.Find ("Main Camera").AddComponent("CamControl");

So far I've got these three files:

  • GenerateMap.cs

  • CamControl.cs

  • Player.cs

Comment
Add comment · Show 5
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 christoph_r · Oct 29, 2014 at 04:29 PM 0
Share

Have you tried using the generic version of AddComponent? It's gameObject.AddComponent();

avatar image Bunny83 · Oct 29, 2014 at 04:39 PM 0
Share

So how does for example your "Player.cs" script looks like? Are you sure it contains a component called "Player"?

It has to look like this:

 // [...]
 public class Player : $$anonymous$$onoBehaviour
 {
     // [...]
avatar image Mobilpadde · Oct 29, 2014 at 04:50 PM 0
Share

@Bunny83 Stupid me, you're right! It doesn't contain anything even remotely related to the word player. I'd forgot I renamed the file a bit earlier... Thanks!

avatar image Bunny83 · Oct 29, 2014 at 04:52 PM 0
Share

@$$anonymous$$obilpadde: So should we close this question or do you want to accept Gardosen answer?

avatar image Mobilpadde · Oct 29, 2014 at 04:55 PM 0
Share

@Bunny83, I'd really like to credit you, but as you haven't answered my question I think I'm going to go with @Gardosen's answer if that's alright with you? (As it also taught me something)

1 Reply

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

Answer by Gardosen · Oct 29, 2014 at 04:30 PM

if you want to attach a script to a gameobject by his name us this

 GameObject.Find("Main Camera").AddComponent<CamControl>();


Greetings Gardosen

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 Bunny83 · Oct 29, 2014 at 04:41 PM 0
Share

That's for sure the better way of attaching a Component to a gameobject. However his method should work as well. If it doesn't he probably has something wrong in those scripts / classes.

@$$anonymous$$obilpadde: Just to explain why this is the better way:

If the class "Player" or "CamControl" doesn't exist you will get a compiler error. If you use the string version you won't get any errors until you actually run that line of code and it will produce a runtime error.

A lot people see error messages as something "bad". But it's actually the other way round. Errors and error messages are there just to tell you that you do something wrong here. Errors are almost never produced by the compiler or the runtime, responsible for any kind of error in almost all cases is the "human factor".

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.

Welcome to Unity Answers

If you’re new to Unity Answers, please check our User Guide to help you navigate through our website and refer to our FAQ for more information.

Before posting, make sure to check out our Knowledge Base for commonly asked Unity questions.

Check our Moderator Guidelines if you’re a new moderator and want to work together in an effort to improve Unity Answers and support our users.

Follow this Question

Answers Answers and Comments

5 People are following this question.

avatar image avatar image avatar image avatar image avatar image

Related Questions

AddComponent not working 3 Answers

Adding a component in the editor with a script 0 Answers

GO.AddComponent just adds an empty Script 1 Answer

AddComponent deletes the game object. 1 Answer

C# - Disabled Script Still Runs! 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