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 /
  • Help Room /
avatar image
5
Question by TheDragonBreather · Sep 01, 2014 at 03:03 AM · component

Can't add script problem

I don't understand this. I really don't. This question has been asked before, I know it has but I'm not finding the answer I'm looking for that would with my predicament. Whenever I try to add a script to an object, I get this, Can't add component "so so" because it doesn't exist. Check to see if the file name and class name match. Makes no sense to me. As far as I'm concerned with file names class names, it's confusing the heck out of me. And I'm stating this now, it's not a "one script problem" thing. It's doing the same thing with all the other scripts I have except for the ones that have already been applied to objects, which means I'm not going to post every single code I have. And some of them haven't been completed or started yet (the reason why is because I just want it already created so I don't forget later), which makes even less sense, so it would be pointless for me to do that.

So, in a nutshell, I would appreciate it and be grateful if someone can shed some light on what to do here, please. Thank you.

Comment
Add comment · Show 2
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 Scribe · Sep 01, 2014 at 04:35 PM 0
Share

Are you able to create a .unitypackage file and upload it somewhere like dropbox? It will probably help us help you!

avatar image ModernAlchemist · Jul 09, 2015 at 03:44 PM 0
Share

I had the same issue when declaring delegate types in the namespace scope which used default parameters. Visual Studio was okay with it, but Unity didn't like it.

13 Replies

· Add your reply
  • Sort: 
avatar image
16

Answer by R3DRAW STUDIOS · Sep 01, 2014 at 05:53 AM

Bold words or symbols = Important word or symbol

ok so the code class must match your file name on the 4th line (example: Name of script is Gear so on line 4 it should say public class Gear : MonoBehaviour )

also in coding at end of codes shold have a semicolon(;) Example:

this is not a code just an example;

also the codes must be in a void and the void must be held by the 4th line using { and }

Example:

 public class Gear : MonoBehaviour{

 void NameWhatEver_NoSpaces{

 above the symbol is counting what im saying now as part of code;

 }   <-----those ended the two above basically simple organizing.

}

hope this helps :D

Comment
Add comment · Show 6 · 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 TheDragonBreather · Sep 01, 2014 at 03:09 PM 0
Share

I appreciate it but I still don't think it has to do with the code. Here, this is one of them.

 using UnityEngine;
 using System.Collections;
 
 public class icesword : $$anonymous$$onoBehaviour 
 {
     // Update is called once per frame
     void Update(){
         if(Input.Get$$anonymous$$eyDown($$anonymous$$eyCode.LeftShift)||Input.Get$$anonymous$$eyDown($$anonymous$$eyCode.RightShift));{
             float distance = 10.0f    
                 // attach IceSword to Clark Eagleheart
                 // make IceSword appear otherwise don't appear
                 ;}
     }
 }

This is one without any errors or anything in particular or noticeable. I even went as far as to name every single thing related to script with the same name (whether it was capital or not) and still can't the script. The only logical explanation for why this is happening is when I updated Unity, the script didn't follow. $$anonymous$$akes no sense to me but I really don't think it is a problem within the code.

avatar image Scribe · Sep 01, 2014 at 03:19 PM 0
Share

And this code is named 'icesword.cs' ?

avatar image TheDragonBreather · Sep 01, 2014 at 04:04 PM 0
Share

yes it is

avatar image TheDragonBreather · Sep 01, 2014 at 04:16 PM 0
Share

nope, still didn't work. this is getting too weird

avatar image BMayne · Sep 01, 2014 at 04:18 PM 1
Share

You have to fix ALL your errors before you can do anything. The project compiles as a whole not per script. Look at your debug log and fix all the errors.

Show more comments
avatar image
1

Answer by BMayne · Sep 01, 2014 at 04:09 PM

You have a bunch of syntax errors. You will not be able to attach scripts that will not compile. You can look what I posted below. I also reformatted it since yours was hard to read.

 public class icesword : MonoBehaviour 
 {
     // Update is called once per frame
     void Update()
     {
         if(Input.GetKeyDown(KeyCode.LeftShift)||Input.GetKeyDown(KeyCode.RightShift)); // <-- Don't place a semicolon here
         {
             float distance = 10.0f  // <-- Missing a semicolon
                 // attach IceSword to Clark Eagleheart
                 // make IceSword appear otherwise don't appear
        ;} //<-- Again with a random semicolon
     }
 }

If you place a semicolon at the end of an if statement it just quits right there. Nothing will get called between the braces (true or not true)

regards,

Comment
Add comment · Show 10 · 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 Scribe · Sep 01, 2014 at 04:34 PM 0
Share

There aren't actually any 'syntax' errors in his code and so it should not stop the code compiling. Ending the if statement early will still compile, just with the wrong behaviour, and the semi-colon is actually in the 'logically' correct place to end the float distance = 10.0f line, as it is not commented out, it is simply the next character and will act the same as if it was written immediately after as you have suggested.

Certainly these are mistakes or bad habits but I do not think they would stop the script compiling!

avatar image TheDragonBreather · Sep 01, 2014 at 05:11 PM 0
Share

I'd appreciate all the help you guys have given but I want to re$$anonymous$$d you guys that it's not just this code. It's doing the "Can't add script" thing with all the other scripts that haven't been applied yet. So, I really don't think it is a problem within the code but a bug within the program.

avatar image gjf · Sep 01, 2014 at 05:16 PM 0
Share

while you have errors in any script, you can't add anything to objects.

avatar image TheDragonBreather · Sep 01, 2014 at 05:28 PM 0
Share

elaborate please

avatar image gjf · Sep 01, 2014 at 05:38 PM 0
Share

do you have any errors? if yes, fix them all before you start adding any scripts to the objects.

Show more comments
avatar image
1

Answer by Cereal_Killa · Jan 17, 2015 at 03:40 AM

When I had this error it was because the class name and script name didn't match. Try using copy/paste to ensure they are EXACTLY the same.

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 MozenAhl · Oct 13, 2015 at 04:44 PM 0
Share

I had the same problem but I fixed it. Here is how you do:

Go to the gameobject you wanna add the script to and click on add componet. Add new script, name the Script (If u want) and start coding from their. When you open the Script it will not be in the Script folder so you'll have to drag it their. It works for me so it should work for you.

Sorry for my english, I'm from Sweden

avatar image
0

Answer by mr_freedo · Oct 21, 2014 at 06:04 PM

I get the same error before I'v even edited my scrit so it cant be an error in the code. Have you moved/delited anny of the unity files so that that miht cause an error? I also renamed my script after creating it witch created an error since the scrit name does not atomaticly cange in the code.

Comment
Add comment · 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
0

Answer by humain1999 · Feb 07, 2015 at 06:25 PM

Remove the space in the name. it worked for me.

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 Gxdzilla_ · Dec 28, 2020 at 07:40 PM 0
Share

none of ur solutions are working, what should I do???? this is my code pls check for errors : using System.Collections; using System.Collections.Generic; using UnityEngine;

public class Enemy : $$anonymous$$onoBehaviour

{ [SerializeField] private float _speed = 4.5f;

 // Start is called before the first frame update
 void Start()
 {
     
 }

 // Update is called once per frame
 void Update()
 {
     transform.Translate(Vector3.down * _speed * Time.deltaTime);
     
     if(transform.position.y < -5f)
     {
         float randomX = Random.Range(-7f, 7f);
         transform.position = new Vector3(randomX, 7, 0);
         

     }
 }

}

  • 1
  • 2
  • 3
  • ›

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

22 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

Related Questions

Problem with gravity with regidbody. 2 Answers

Component order on a game object changed on unity editor restart 0 Answers

Disable part of script 2 Answers

c# animation.Play("animation") 0 Answers

Why can't you add editor scripts to GameObjects even if the tag is EditorOnly? 0 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