Unity 4 - JavaScript work, but CSharp does not... please help.

Probably a very noob question, but I ve been struggling on it for hours now.

The problem I am having is when I apply a CSharp script, it keeps giving me this warning: – The associated script can not be loaded – Please fix any compile errors – and assign a valid script

The script is right because I got it from Unity tutorials, the java version work fine but the C# never does…

I tried different things such as running as Administrator (Windows 7) Or turning off my Antivirus… but nothing seem to help, any help would be really appreciated.

I am fairly new to Unity, but I m sure this shouldn’t be to hard to fix, but I cant seem to find an answer on Google, so I hope someone here will be able to help.

Thank you

–That s the script that I cant get to work in C#, but the Java version work fine.

using UnityEngine;
using System.Collections;

public class ExampleBehaviourScript : MonoBehaviour
{
    void Update ()
    {
        if(Input.GetKeyDown(KeyCode.R))
        {
            gameObject.renderer.material.color = Color.red;
        }
        if(Input.GetKeyDown(KeyCode.G))
        {
            gameObject.renderer.material.color = Color.green;
        }
        if(Input.GetKeyDown(KeyCode.B))
        {
            gameObject.renderer.material.color = Color.blue;
        }
    }
}

Your code is fine. Changing colors depends on the shader having an _Color properties. Check your shader. If in doubt, start with the Diffuse shader (not Default Diffuse).

Hi!

First: It is not Java it is JavaScript. There is a huge difference. Also the JavaScript implementation of Unity is not normal JS. THere are also some differences. You can call it UnityScript.

It seems that this is a little bug in Unity. Read this Thread from the Unity Forums. There are many solutions for your problem.

First - your script file must have the same name as the main class in this file. So your filename must be ExampleBehaviourScript.cs (The .cs is hidden in the project view but you see it in the status bar at the bottom of the project view.

If there is everything ok, it may be a Unity Bug. Unity saves meta-files for all imported assets - also scripts. You need to delete this files for all scripts so Unity reimports them. To do this go into Edit > Project Settings > Editor and then under Version Control set the mode to Visible Meta Files.

Open the script directory by right clicking on the script in the project view and choose “Reveal in …” where … is Finder or somethign else, depending on your OS.

Then delte all .meta files of your scripts.