- Home /
Assembly reference problem (C#)
I have this script:
using UnityEngine;
using UnityEngine.Sprite;
public class SpriteLightColor : MonoBehaviour {
public SpriteObject sr;
void Start () {
sr = getComponent<SpriteRenderer>();
sr.color=directionalLight.color;
}
}
But throw this following errors:
Assets/Scripts/C Sharp/SpriteLightColor.cs(6,9): error CS0246: The type or namespace name `SpriteObject' could not be found. Are you missing an assembly reference?
and:
Assets/Scripts/C Sharp/SpriteLightColor.cs(2,1): error CS0138: A `using' directive can only be applied to namespaces but `UnityEngine.Sprite' denotes a type. Consider using a `using static' instead
I cannot make this script work. Any idea how to fix it?
Thanks.
Sorry for my bad english :S
Answer by JDelekto · Jun 27, 2018 at 01:02 AM
1) Remove the using UnityEngine.Sprite;
2) change public SpriteObject sr;
to be public SpriteRenderer sr;
3) Provide a declaration for "directionalLight" (not sure what type this should be, but apparently an object with a property named "color" whose type is "Color").
still no working...
Assets/Scripts/C Sharp/SpriteLightColor.cs(9,8): error CS0103: The name `getComponent' does not exist in the current context
It looks like you might have copied your example from JavaScript, where the coding convention is to use camel-casing for method names, i.e. getComponent()
.
However, in the C# realm, method names are Pascal-cased, so you will want to use GetComponent()
as this method is case-sensitive.
Depending on the IDE you are using to write your scripts, there should be some type of Intellisense....
Now doesn't throw any error, but doesn't work as espected. btw I use Notepad++, since the deprecation of $$anonymous$$onoDevelop on unity. Now we have that trash of Visual Studio.