-1

I'm trying to create a UnityScript. When the player hovers their mouse over a cube, it goes green. If the player does OnMouseDown, the color will go red. If the Player does OnMouseUp, the color will go blue.

I'm getting an error where it claims as follows:

kkjk.js(14,43): BCE0044: expecting :, found '='.

Here is my code.

 #pragma strict public var Cube : GameObject; var cubeRenderer : Renderer; function Start() { //Get renderer component of the cube. cubeRenderer = GetComponent(Renderer); } function OnMouseOver() { { transform.renderer.material.color = Color.green; //Line where the error is occuring. if (Input.GetMouseButtonDown); OnMouseDown(); } function OnMouseDown() { { transform.renderer.material.color = Color.red; if (Input.GetMouseButtonUp); GetMouseButtonUp(); } function GetMouseButtonUp() { } transform.renderer.material.color = Color.blue; } 

Lots of appreciation if someone could help me on this one, I am using UnityScript, it's just easier and a bit less complicated than C#.

5
  • 5
    Is there any specific reason why you still use unityscript in 2025 ...? That stuff has been depreciated since 2018 ... Go for c#! To be fair I don't really come from a JavaScript background but I don't see any reason why someone would claim JS is less complicated than c# .. I find it horrible :'D .. and on top unityscript is a weird thing .. not really JS either so you basically live between two worlds .. not really using the full potential of either Commented May 11 at 20:37
  • Far guess would be that maybe in JS/UnityScript you would first need to assign the returned Material to a var and then modify and write it back ..? But no clue tbh ^^ as said.. c# guy here and that thing was in deprecation ever since I got into Unity so I never went deep into it ;) Commented May 11 at 20:42
  • Im surprised transform.renderer is valid, as not everything has a renderer so I wouldnt expect it to be exposed Commented May 11 at 21:26
  • @BugFinder in old Unity versions that was also the case in the c# API though .. Component had properties for most of the "standard" built-in components like renderer, collider .. even camera .. even though marked as deprecated they kept them around for a long time actually.. which was a bit annoying when you called your own fields (or e.g. loop variable) renderer as it was then hiding that built-in deprecated one and IDEs would cry about it ^^ Commented May 12 at 4:34
  • But yeah to hook up to @BugFinder 's comment: didn't you anyway use cubeRenderer = GetComponent(Renderer); .. so why not just use that reference you got already? And yeah the direct properties might only be available in MonoBehavior (or in your case simply your own Unityscript components) but not in the built-in ones that often only inherit from Component .. not sure Commented May 12 at 4:46

1 Answer 1

1

As said I have not really a clue about the UnityScript specifics and potential special syntax traps

But you already get a hold of the Renderer in

cubeRenderer = GetComponent(Renderer); 

so I suppose in general instead of trying to go through

transform.renderer 

you further on rather would want to use that cubeRenderer

Just for testing I installed Unity 2018.1 where it actually automatically updates the API from the deprecated transform.renderer to transform.GetComponent.<Renderer>().

Even in older c# API - from around the time UnityScript also got deprecated - these direct properties existed but were already deprecated

Which would be indicating you are using an even older version!


Anyway, I copied your exact provided code and got all kind of other syntax errors - not specifically the one you mention.

Among these e.g. it should also not be

 if (Input.GetMouseButtonDown); // also note weird placed ; here 

but rather

if (Input.GetMouseButtonDown(0)) { ... } 

fixing both, syntax and logic errors.

And then also there is a couple of misplaced { } in e.g.

function OnMouseOver() { { 

causing some of your methods being local functions embedded within others

It's a bit unclear what exact behavior you are trying to achieve with the coloring but this e.g. works for me now after fixing all mentioned issues:

#pragma strict public var Cube : GameObject; var cubeRenderer : Renderer; function Start() { //Get renderer component of the cube. cubeRenderer = GetComponent(Renderer); } function OnMouseEnter() { cubeRenderer.material.color = Color.green; } function OnMouseExit() { cubeRenderer.material.color = Color.white; } function OnMouseDown() { cubeRenderer.material.color = Color.red; } function OnMouseUp() { cubeRenderer.material.color = Color.blue; } 

In general as said UnityScript is dead! This was deprecated in 2018 and I strongly recommend you drop it and get into c# instead!

You still using UnityScript also implies you are using a way out of date Unity Version from 2017!

To be fair I'm not coming from a JavaScript background but IMHO c# only has advantages over UnityScript:

  • supported by all decent IDEs
  • better debugging
  • broader API
  • maintained Unity API
  • maintained Unity Version in the first place!
  • (mostly) applicable outside of Unity
  • direct hook into asynchronous coding, native plugins, strictly typed
  • etc
Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.