My visual studio script is not working in Unity - c#

I just started game development and got into a problem
whenever I load my Visual Studio code into Unity it says
The refrenced script unknown on this behaviour is missing
This is my script
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class VirtualManMovement : MonoBehaviour
{
// Start is called before the first frame update
void Start()
{
Debug.Log("Hello, World!") ;
}
// Update is called once per frame
void Update()
{
}
}
I need a solution for this

This may happen is the file name does not match the class name. Make sure that the file containing this script is also named VirtualManMovement.cs

Make sure in Unity you go to edit -> preferences and see that you have Unity External tools in Your IDE Selected.
Ensure your Code File Name is the same as your Class to prevent confusion.
I was confused as well when I first started. Hopefully, you find this useful

Related

Unity- vs code error: if ($?) { dotnet run $Player_Movement } (Solved)

Hi I am getting "cd "c:\Users\UMUT\Desktop\Unity Projects\Newest\Assets" ; if
($?) { dotnet run $Player_Movement }" message and I get this error message "Couldn't find a project to run. Ensure a project exists in C:\Users\UMUT\Desktop\Unity Projects\Newest\Assets, or pass the path to the project using --project." in my Vs code Unity project. İn my another old project I have a C# script named Player_Movement but I am getting this error in my new Unity Project when I try to debug the project.I downloaded C# extension.I downl Any solutions or reason why it is happening?
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class New : MonoBehaviour
{
// Start is called before the first frame update
void Start()
{
Debug.Log("hi");
}
// Update is called once per frame
void Update()
{
}
}
I resetted Vs code settings and change my C# extension to older version and error fixed.
I reseted Vs code settings to default and my problem is fixed.

Unity: Calling a method from another script

I am completely new to Unity and all other answers I've found for this go over my head.
So far I've run everything from the same script, which is getting very big and messy. Therefore I am trying to learn how to call methods from other scripts.
I have a dropdown menu with the code in one script and I'm trying to call that code from another.
ScriptA:
using UnityEngine;
public class ChoseLanguage: MonoBehaviour
{
public TMPro.TMP_Dropdown myDrop;
DisplayController displayController;
public void DropdownChooseLanguage()
{
if (myDrop.value == 1)
PlayerPrefs.SetString("chosenLanguage", "Spanish");
if (myDrop.value == 2)
PlayerPrefs.SetString("chosenLanguage", "Japanese");
if (myDrop.value == 3)
PlayerPrefs.SetString("chosenLanguage", "Korean");
if (myDrop.value == 4)
PlayerPrefs.SetString("chosenLanguage", "Icelandic");
Debug.Log(PlayerPrefs.GetString("chosenLanguage"));
displayController.DropdownSetLanguage();
}
}
The selection code works by itself, and the debug.Log shows that the chosen language is being saved correctly to PlayerPrefs.
The error comes when it tries to read the "displayController.DropdownChooseLanguage();" line. (Line 28)
Unity gives this error:
NullReferenceException: Object reference not set to an instance of an object
ChoseLanguage.DropdownChooseLanguage () (at Assets/Scripts/ChoseLanguage.cs:28)
Script B
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
using System.Linq;
using TMPro;
public class DisplayController : MonoBehaviour
{
...
public void DropdownSetLanguage()
{
SetFileName();
setLanguage.gameObject.SetActive(false);
Start();
}
...
}
Earlier, the exact same code from Script A was placed in ScriptB and all the code worked as it should.
This is a very noob question but I have simply just never been able to understand how exactly to access other scripts correctly.
Any help will be greatly appreciated.
Thanks.
EDIT:
I found a solution to this but I'll keep the question up in case other beginners have the same problem or if anyone has a better solution.
I made the DisplayController displayController; into public DisplayController displayController; and then dragged the gameobject with the displaycontroller script attached into the slot for it.
DropdownSetLanguage is public and it seems you think you can access it by
creating a DisplayController object named displayController.
It is ok
about to access a class but, displayController should be null in this state.
Because you have to assign an initial DisplayController to displayController
in your first code.
You can add public or [Serializable] tag in front of
DisplayController displayController; in your first code then, you can add
a GameObject which has a DisplayController script in it in Unity editor.
accessibility

Visual Studio Intellisense for UnityEngine not working

I am trying to write a script for Unity in C#. I created the script and then I opened it. When I typed in Ge, I don't get Unity methods (I only see GetHashCode and GetType). I am looking for GetComponent. I tried writing the script (from a tutorial) and it works fine in Unity, so only the intellisense is not working. It also doesn't work for new Vector3().
I am using Visual Studio Community 2019 (v 16.5.2).
The complete script is:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class AddConstantVelocity : MonoBehaviour
{
// Start is called before the first frame update
void Start()
{
}
// Update is called once per frame
void Update()
{
GetComponent<Rigidbody>().velocity = new Vector3(2, 10, -6);
}
}
You need to specify VS Code as the default editor.
Edit > Prefernces > External tools > External Script Editor

Monobehaviour scripts in the file, or their names don't match the file name on newly made script

Why all my newly created script always have this error? Even though i had put in my intended code, all my script always show like this.
my code is this:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class Testing : MonoBehaviour
{
public GameObject obj;
// Start is called before the first frame update
void Start()
{
obj = GetComponent<GameObject>();
}
// Update is called once per frame
void Update()
{
}
private void OnTriggerEnter2D(Collider2D collision)
{
collision.transform.position = obj.transform.position;
}
}
Welcome to Stackoverflow!
This issue is mostly caused when you create a script outside the Unity Editor, i.e. in your IDE or editor of choice. Whenever you want to create a script, you need to create it inside the Editor, by right-clicking in your project folder and selecting "New C# Script".
If you do create your script inside Unity, you need to make sure that the class name (in your case, "Testing") is the same as the script file name.
Regardless of whether these solutions work for you, please make sure to search for answers before posting, as there are multiple duplicate issues in the Unity forums.

Unity does not even recognize "Collider" on my function "OnTriggerEnter()"

I'm a Unity beginner ,so right now I'm doing some small projects by watching some tutorials on youtube.
But, after looking at one tutorial that used the "OnTriggerEnter" function with the attribute "Collider". I realized that my editor didn't recognize "Collider" so I couldn't use "OnTriggerEnter".
I searched on the internet, but coudln't find any answer related to my question.
It's the first time, it's happened to me so I don't really know how to solve this problem...
Here's my little code, I'm just trying to move something when my player enters in a zone.
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class Trigger : MonoBehaviour
{
public bool opening = false;
// Start is called before the first frame update
void Start()
{
}
// Update is called once per frame
void Update()
{
}
// OnTriggerEnter is called when something enter in the trigger
void OnTriggerEnter(Collider obj)
{
if(obj.transform.name == "Player")
{
opening = true;
}
}
}
So, as I said, my unity editor which is Microsoft visual studio does not detect "Collider" (I don't have the possibility to pre-fill by pressing enter for example) and the color is white and not blue like "true" for example.
So because of that I can't move forward with the project, and I'd like to know what to do so that my editor recognizes "Collider" and I can make my project work!
Thank you in advance for your answers.
To solve that you can do the following:
Close Unity and Visual Studio.
Open Unity.
Open Visual Studio from Unity (Assets => Open C# Project).
If that doesn't work you need to check your current editor in:
Edit => Preferences => External Tools => External Script Editor
Visual Studio always have some errors like missing dependencies and stuff. I would recommend using Visual Studio Code application. Just like #Jack has explained. Browse to Edit => Preferences => External Tools => External Script Editor and change your script editor to Visual Studio Code and then try again.

Categories