Image of my code in Visual Studio
Forgive me, I am somewhat new to coding, so maybe this is a dumb question.
I am learning how to use Unity and I am using Visual Studio to edit my code (C#).
For some reason, the red squiggly lines will not appear for most errors. It won't even recognize that there is an error.
The only error I have had it recognize is a missing semicolon.
For example, I can say a string is equal to a float (as seen below: characterName = itemDurability;), and it had not issue with this in either Visual Studio or VSCode. I can have an item defined as multiple different things, and still no errors. Of course Unity will tell me there is a problem when the code loads, but I'd like to know as I'm writing it.
I have tried updating and reinstalling, but nothing works. I cannot find anything online that has helped me with this problem.
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class Character : MonoBehaviour
{
// Start is called before the first frame update
void Start()
{
string characterName;
characterName = "Duncan";
int characterLevel = 5;
int experience = characterLevel * 5;
float itemDurability = 1.527f;
experience = (int)itemDurability;
characterName = itemDurability;
bool equippable = false;
if (itemDurability > 1f)
{
experience = (int)(itemDurability * 1.5f) / characterLevel;
}
}
}
From your screen-shot, it appears that you have opened the .cs (using VS) by double clicking on it or that file is not part(or excluded) of your Project , essentially you're opening the file for editing and using VS like a text editor as Indicated by Miscellaneous Filesand Attach Debugger symbol from your snap
Ensure that File is added to your Project with SolutionExplorer window and BuildAction=C# Compiler. See example
The only error I have had it recognize is a missing semicolon.
Visual Studio IDE interpreted the file as C# from the .cs extensions, and because of which it will look for ; and a semicolon is required at the end of every statement in C#.
For some reason, the red squiggly lines will not appear for most errors
Compiler is only doing a syntax check and not checking the semantics of the statements, for that matter compiler will even accept something like
Myclass a = new MyClass(); even though MyClass is not defined
Related
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.
I am trying out Unity for my Game Design course, but I can't seem to get the code to work. I suspect it is because Visual Studio (which I write my code on) is not connecting to Unity properly, and here's why I think this:
For one thing, whenever I try to put the code on an object, it prints out this error:
Can't add script behaviour CallbackExecutor. The script needs to derive from MonoBehaviour!
This is despite the fact that nowhere is the code called CallbackExecutor, and the script apparently does derive from MonoBehaviour.
Second, when I load up the code onto Visual Studio, I get this error message:
C:\Users\cemya\Documents\D&D Roguelight Project\Assembly-CSharp.csproj : error : The project file could not be loaded. Could not find file 'C:\Users\cemya\Documents\D&D Roguelight Project\Assembly-CSharp.csproj'. C:\Users\cemya\Documents\D&D Roguelight Project\Assembly-CSharp.csproj
The problem is I don't know why it's not connecting. There's no option to import the package (which implies it's already imported), and I do have a package for Unity.
I'm using Visual Studios 2017 (specifically, I'm using the 2d setup most of the time), Unity version 2018.2.4f1, and the package is called Visual Studio 2017 Tools for Unity [Experimental], which is what was automatically downloaded when I began to set up coding for Unity. Since I got this version automatically, I believe that this should be a common problem, but I can't seem to find real answers on it (I even asked on the Unity forms themselves!)
I can show you the code if you think that would help.
UPDATE: I have just updated Unity to 2018.2.5f1 and tested it with the code that has actual code. All that happened is that the name for the error message is different:
Can't add script behaviour TMP_CoroutineTween. The script needs to derive from MonoBehaviour!
Also, here is the code for the one I'm testing, if it helps:
public class RollScript : MonoBehaviour
{
Random rand = new Random();
int r = 20;
int m = 5;
// Use this for initialization
void Start ()
{
int rolling = DieRoll(r, m);
print(rolling);
}
// Update is called once per frame
void Update ()
{
}
int DieRoll (int roll, int mod)
{
int get = rand.next(1, roll);
int result = get + mod;
return result;
}
}
UPDATE 2: I have checked with visual studio, and the error message there isn't there anymore, so that's an improvement.
UPDATE 3: I have created a new project and I have not run into issues there. I guess the problem was the code's connection to older editions.
This Can't add script behaviour TMP_CoroutineTween. The script needs to derive from MonoBehaviour! error has been mentioned in a few places on the internet, but with no satisfactory answers. This seems to be the only StackOverflow question about it.
I suspect it happens when there are un-fixed compile-time errors while you're adding a script to a GameObject. I had this problem just now, and after I fixed the errors, I was able to add the script. However, I tried to confirm this by adding a syntax error and then adding a script, but I still didn't get the Can't add script behavior error. So that's odd, but if you're encountering that error, I suggest fixing any compile-time errors and see if that works. (Whether it works or not, please reply to this question with the result.)
I want to use c++ code in c# for Unity using CLR.
The program works properly outside of unity, but inside of engine it gives me an error:
"cs0227: unsafe code requires the 'unsafe' command line option to be specified"
I am really confused, because the project builds successfully in visual studio (without any errors or warnings). I have "allow unsafe" button activated.
using UnityEngine;
using System.Collections;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
public class newspawn_real : MonoBehaviour {
void Start () {
unsafe
{
fixed (int * p = &bam[0, 0, 0])
{
CppWrapper.CppWrapperClass controlCpp = new CppWrapper.CppWrapperClass();
controlCpp.allocate_both();
controlCpp.fill_both();
controlCpp.fill_wrapper();
}
}
}
// ...
}
You have to explicitly enable unsafe code in Unity. You can follow the steps below:
1. First step, Change Api Compatibility Level to .NET 2.0 Subset.
2. Create a file in your <Project Path>/Assets directory and name it smcs.rsp then put -unsafe inside that file. Save and close that file.
Close and reopen Visual Studio and Unity Editor.
You must restart both of them.
It's worth noting that even after doing this and restarting both Unity and Visual Studio, if the problem is still there,
Rename the smcs.rsp file to csc.rsp, gmcs.rsp or mcs.rsp
Restart Unity Editor and Visual Studio each time until you get one that works. For more details about the file name to use, refer to Platform Dependent Compilation documentation.
Simple C# unsafe code that compiles after this.
public class newspawn_real : MonoBehaviour
{
unsafe static void SquarePtrParam(int* p)
{
*p *= *p;
}
void Start()
{
unsafe
{
int i = 5;
// Unsafe method: uses address-of operator (&):
SquarePtrParam(&i);
Debug.Log(i);
}
}
}
update:
on unity 2019 and forward, we don't have the 2.0 subset.
use 2.0 Standard.
and although mcs.rsp works, there is a warning noting that mcs is obsolete, and we should use csc.rsp instead.
but it actually works !
When I try to build my WPF project I get the folowing error:Samotorcan.Client.WPF.Windows8\Views\MainWindow.xaml(1,17): error MC3074: The tag 'Window' does not exist in XML namespace 'clr-namespace:Samotorcan.Client.WPF.Windows8.Controls'. Line 1 Position 17.The problem only occurs when I try to use linq. If I have a line like this new List<object>().ToArray<object>(); somewhere in the MainWindow.xaml.csfile I get the above error and if I change it to new List<object>().ToArray(); the error disappears and the project builds successfully.I also checked the build log and it contains the same error at task MarkupCompilePass2.I am using .NET Framework 4.5 and Visual Studio 2012.EDITAfter playing with it a bit I managed to find out that it's not specificly related to linq an random changes like removing an unused file from a project or just removing parts of code like a simple string creating string s = new String(); fixes the error.At one point a simple change in a constructor from this
public LoggedEventArgs(string message)
{
Message = message;
}
to this
public LoggedEventArgs(string message)
{
// Message = message;
}
fixed the error.
Managed to find a workaround for the above error by moving all my controls from the project into a separate project just for controls.
Now there are no random errors on build because as it looks like it can always find the window and other controls that are now in a separate assembly.
Im writing a policy plugin for VS which checks several issues with the code. If an issue occurs it will be displayed in the policy warnings tab. Now I want to jump to the line where the issue occurs in the editor when I double click it in the policy warning tab. How can I do that?
namespace PolicyPlugin
{
[Serializable]
public class MyPolicyPlugin : PolicyBase
{
//...
//called if the user clicks on a policy warning
public override void Activate(PolicyFailure failure)
{
// make jump to line x
}
}
}
Thanks!
You could try to get DTE automation object first:
EnvDTE.DTE dte = (EnvDTE.DTE)Package.GetGlobalService(typeof(EnvDTE.DTE));
or use alternative ways to get it.
An then execute standard command (that's what happens when you press CTRL+G in Visual Studio)
DTE.ExecuteCommand("Edit.Goto", "1234")
Note: I'm not sure about exact ExecuteCommand method signature. Also you can manipulate IDE the same way for other commands.