Getting errors while creating a custom terrain editor (Unity C#) - c#

I created two scripts called "CustomTerrain" and "CustomTerrainEditor". Here is the code for the latter:
using UnityEngine;
using UnityEditor;
using EditorGUITable;
[CustomEditor(typeOf(CustomTerrain))] // Links editor code in here with CustomTerrain.cs
[CanEditMultipleObjects]
public class CustomTerrainEditor : Editor
{
void OnEnable() {}
public override void OnInspectorGUI() {} // The GUI we will see in the inspector
}
Here are the 2 errors I get:
I don't understand what I am doing wrong. Thanks for the help!

First, change typeOf to typeof.
And next if CustomTerrain in another namespace, add this space to your script.

Related

script class can not be found unity error

i'm new to unity and c# and after dealing with some compiling errors my script was compiling with just some warnings so i tried to put it into a game object and unity said "can't add script "startscreen" because script class can not be found" i searched about this error and all i found is about matching the class name and the file name but i checked so many times and it matches, after i saw about compiling errors but as i said the console only show warnings and finally i tried to delete all and recreate, i tried reimport all and i tried to change project now i realy don't know what to try, here's one of my 3 scripts maybe there is an error but the error massage appears on every script
using System;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class startscreen : MonoBehaviour
{
public event EventHandler switchMenu;
private void ChangeButtonSelected(object sender, EventArgs e){
buttonStart=gameobject.FindGameObjectWithTag("button").GetComponent<button1>();
buttonOther=gameobject.FindGameObjectWithTag("button").GetComponent<button2>();
butonStart.selectChange(e);
butonOther.selectChange(e);
}
void Start(){
switchMenu += ChangeButtonSelected;
}
void Update(){
if(input.GetKeyDown(KeyCode.Left) || input.GetKeyDown(KeyCode.Right)){
if(input.GetKeyDown(KeyCode.Left)){
string key = "left";
}if(input.GetKeyDown(KeyCode.Right)){
string key = "right";
}
switchMenu?.invoke(this, EventArgs.key);
}
}
}
I't very hard to solve the issue with the informations provided. I suggest you to recreate the scripts directly from Inspector Window -> Add Component -> Script component and check if it solve the issue.

Unity: Access HLSL file like a TextAsset?

I would like to expose a public property in my script from which I can drag and drop an .hlsl file onto the script component in the inspector window, like in this image, which shows an exposed property to drop a TextAsset:
The code powering this is:
public class NativeSDKWrapper : MonoBehaviour
{
public TextAsset ShaderFile;
// ...
}
According to the Unity Manual page on TextAsset, .hlsl is not listed as a supported file type. How can I get around this?
I need to read in the contents of an HLSL file to a string, so that I can compile it with D3DCompile.
You can use the Editor class ScriptedImporter to handle this in your editor:
using UnityEngine;
using UnityEditor.AssetImporters;
using System.IO;
[ScriptedImporter(1, "hlsl")]
public class CubeImporter : ScriptedImporter
{
public override void OnImportAsset(AssetImportContext ctx)
{
var ta = new TextAsset(File.ReadAllText(ctx.assetPath));
ctx.AddObjectToAsset("main obj", ta);
ctx.SetMainObject(ta);
}
}
Code based on documentation code linked above.
Be sure to put it in your project's Editor folder, as it references Editor stuff that won't be accessible once built.

Can't add script component 'HealthDisplay' because the script class cannot be found

I have seen similar questions, but I legitimately can't understand my problem here.
I have created a new script called HealthDisplay, from within Unity itself. It generated with some sample code, and I tried to attach it to a UI object that's text - however that error shows up.
I looked up the error and got all sorts of suggestions, however the code was generated by Unity itself, and the name of the class matched the name of the file exactly. I added some more code and tried to attach it then, but it still didn't work. I also tried reinstalling both Unity and the Unity Hub, and still nothing. Here's the code within HealthDisplay.cs:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
public class HealthDisplay : MonoBehaviour
{
private int health = 100;
public Text healthText;
// Update is called once per frame
void Update()
{
healthText.text = "HEALTH : " + health;
if(Input.GetKeyDown(KeyCode.Space))
{
health--;
}
}
}
The file, I've triple checked, is called HealthDisplay.cs. Its name matches exactly. Is there something I'm doing wrong, or could this just be some weird bug with Unity? Thanks.

How can I access the Unity 'Light 2D (Script)' component via Script?

In my Unity 2D game, I have a character with a lightsource component
'Light 2D(Script)' from the Lightweight RP package.
I want to change the intensity of the Light 2D with the code below.
But I can't assign the 'Light 2D(Script)' to the public Light LightSource in the Unity Inspector panel.
I've tried using public Light2D LightSource class but it doesn't seem to exist.
Is there any other way to access the 2D Light component or something I'm doing wrong?
I've also added a screenshot of the Inspector pane, if it helps.
If there's any more information you need just tell me and I hope someone can help. Thanks
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class Lighting : MonoBehaviour
{
public Light LightSource;
public float lightIntensity;
public float minIntensity = 0.35f, maxIntensity = 0.65f;
void Update()
{
lightIntensity = Random.Range(minIntensity, maxIntensity);
LightSource.intensity = lightIntensity;
}
}
Character Inspector Screenshot
Assuming you are using the universal render pipeline for 2D lighting this is the solution I found.
Within the Light2D script (which you can open by clicking the three vertical dots in the top right of the Light2D component and hitting edit script) there is the declaration of the namespace UnityEngine.Experimental.Rendering.Universal In order to reference Light2D simply at the top of your script write
using UnityEngine.Experimental.Rendering.Universal;
There may appear to be an error when doing this, but press on. From here on out you'll be able to reference the Light2D component much as you would any other component, such as using GetComponent<Light2D>() If any of this gives an error within your code editor still compile to test if it works, I have a fully functioning script using this method but Visual Studio still believes that Light2D does not exist. Regardless, this appears to work and I hope this works for everyone else as well.
Ok bud i got your answer.
First off, u need to open the Light2D(script) do the following:
1.) add the following to the top of the script:
using System.Collections;
using UnityEngine;
2.) you need to set the class of the Light2D script to "public" (so u can access it in your "lighting.cs" script.):
public class Light2DManager : IDisposable
3.) once that is done save the light2d script.
Ok now we are going to access it...
in your Lighting script we do the usual when we want to access another script:
GameObject TheLight = GameObject.Find("The Gameobject the script is on");
UnityEngine.Experimental.Rendering.LWRP.Light2D The2DLights = TheLight.GetComponent <UnityEngine.Experimental.Rendering.LWRP.Light2D> ();
Now you should have access, just type the following to confirm:
The2DLights. (and you will get options like intensity etc etc)
This is my first time ever answering a question. turns out i was having the same issue, happy to help.
Ran into this today, to expand on this solution of the using statement (simply adding it did not work in my case, on 2020.2) at the top you can do:
using Light2DE = UnityEngine.Experimental.Rendering.Universal.Light2D;
Then use Light2DE as the class instead of Light2D in your script.
light = transform.GetComponent<Light2DE>();
I don't understand the question very clearly, but it's possible you're just looking for "GetComponent".
Here's a random example,
public class CamToUI:MonoBehaviour
{
[System.NonSerialized] public WebCamTexture wct;
public Text nameDisplay;
private RawImage rawImage;
private RectTransform rawImageRT;
private AspectRatioFitter rawImageARF;
private Material rawImageMaterial;
void Awake()
{
rawImage = GetComponent<RawImage>();
rawImageRT = rawImage.GetComponent<RectTransform>();
rawImageARF = rawImage.GetComponent<AspectRatioFitter>();
}
GetComponent finds "thing" (such as a "light2D") which is attached to that same game object.
• You have some game object
• A script, Script.cs, is attached to that game object
• Some other thing (say, Light2D) is attached to that same game object
Inside the script Script.cs, you can find the other thing, using GetComponent.
Ideally, don't call GetComponent repeatedly, just call it once in Awake or whatever. (But you're a mile away from worrying about performance, so don't worry about it.)
using UnityEngine.Experimental.Rendering.Universal;
using UnityEngine;
public class Light2DController : MonoBehaviour {
void Awake() {
//assuming You have Light2D component in the same object that has this script
Light2D light = transform.GetComponent<Light2D>();
}
}
Unity 2019.3.0f1
Here's how you can access the Light2D(Script) components.
Unity Version: Unity 2020.1.4f1
Import using UnityEngine.Experimental.Rendering.Universal; into your Script.
Please find the below code example.
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.Experimental.Rendering.Universal;//Important
public class Lighting : MonoBehaviour
{
Light2D theLights; // To Access the Light Components.
public float maxRange = 0.54f;
public float minRange = 0.31f;
public float flickerSpeed = 0.5f;
void Update()
{
//Assuming You have Light2D component in the same object that has this script.
theLights.intensity = Mathf.Lerp(minRange, maxRange, Mathf.PingPong(Time.time, flickerSpeed));
// The above Line is to create a flickering effect, You can also set a value like for example: theLights.intensity=0.5f, To set the intensity of the Light.
}
}
You can also access other Light2D components similar way
theLights.color = new Color(0, 255, 0);// here I am accessing color and setting a new color value.
In newer versions of unity (i'm using 2021.2.7f1) use this:
using UnityEngine.Rendering.Universal;
And then in code:
Light2D light = gameObject.GetComponent<Light2D>();

The type or namespace name `Kinect' does not exist in the namespace `Microsoft'

I am using Unity to develop a game and I want to use Kinect so I add a reference to Microsoft.Kinect.ddl and I have the following code (nothing impressive):
using UnityEngine;
using System.Collections;
using Microsoft.Kinect;
public class Main : MonoBehaviour {
// Use this for initialization
void Start () {
print(KinectSensor.KinectSensors.Count);
}
// Update is called once per frame
void Update () {
}
}
Visual Studio does not mark any error but when I am trying to run it using Unity I am getting a compilation error The type or namespace name Kinect does not exist in the namespace Microsoft . Any idea how can I fix that?
See this page for using the SDK with unity. It does have some obvious errors though (as pointed out in the documentation.)
I am using k2examples asset from the asset store, and I ran into the same problem... since you are not using the same asset, this might not solve the problem, but you can give it a try:
import the standard assets.

Categories

Resources