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.
Are you able to create a .unitypackage file and upload it somewhere like dropbox? It will probably help us help you!
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.
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
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.
nope, still didn't work. this is getting too weird
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.
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,
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!
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.
while you have errors in any script, you can't add anything to objects.
do you have any errors? if yes, fix them all before you start adding any scripts to the objects.
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.
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
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.
Answer by humain1999 · Feb 07, 2015 at 06:25 PM
Remove the space in the name. it worked for me.
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);
}
}
}