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.)
Related
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
I have a PoC to use some existing Java-codebase in some UWP-app using the most current Visual Studio Community 19 version 16.3.2 and the latest released IKVM 8.1.7195.0. The app builds and runs fine in Debug-mode, but fails to build already in Release-mode with the following error:
MCG0004:InternalAssert Assert Failed: ICE: trying to add a local var
with the same name, but different types. during
[_RegisterClipboardFormat] Ams.Oms.Poc
RegisterClipboardFormat is part of IKVM:
#DllImportAttribute.Annotation(value = "user32.dll", EntryPoint = "RegisterClipboardFormat")
private native static int _RegisterClipboardFormat(String format);
#cli.System.Security.SecuritySafeCriticalAttribute.Annotation
private static int RegisterClipboardFormat(String format)
{
return _RegisterClipboardFormat(format);
}
https://github.com/ikvm-revived/ikvm/blob/master/openjdk/sun/awt/IkvmDataTransferer.java#L95
What I'm wondering is which local variable the error message is referring to? Might be something added implicitly or might have to do with String in Java vs. string in C#? OTOH that file is clearly named .java.
Didn't find much about the error message in general, only the following two links seems to be more interesting:
Variables having same name but different type
Why doesn't C# allow me to use the same variable name in different scopes?
So I'm currently even unsure where the message comes from, Visual Studio/C# directly or IKVM during running code during building Release-mode. I strongly suspect the error is coming from Visual Studio/C#, though.
Searching for the function itself doesn't reveal much of help as well:
Sorry, AWT is not a supported part of IKVM.
https://sourceforge.net/p/ikvm/bugs/225/
Others seemed to have the same problem, because CN1 simply disabled that code entirely in their fork of IKVM:
//#DllImportAttribute.Annotation(value = "user32.dll", EntryPoint = "RegisterClipboardFormat")
//private native static int _RegisterClipboardFormat(String format);
#cli.System.Security.SecuritySafeCriticalAttribute.Annotation
private static int RegisterClipboardFormat(String format)
{
throw new Error("Not implemented");
//return _RegisterClipboardFormat(format);
}
https://github.com/ams-ts-ikvm/cn1-ikvm-uwp/blob/master/openjdk/sun/awt/IkvmDataTransferer.java#L95
Any ideas? Thanks!
There seems to be a workaround by not changing any code at all: The settings of the Release-build contain a checkbox if to use the .NET native toolbox for the build, which is enabled by default. By disabling that the build succeeds without any code change and is as fast as the Debug-build again. Before changing that, the Release-build took a lot longer as well.
Don't know what that means regarding actually calling native code, if that fails or not, because my app doesn't use those. I guess it would fail, depending on if it works in Debug or not. Additionally, I'm not sure if the Windows store accepts such a modified Release-build, but as UWP-apps aren't forced to use native code at all, I guess there's a good chance things are going to work.
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 have just started using Parse.com On Unity 5.0.0fb, and after getting my application to work with Parse on Unity Editor I decided to try it on my Mobile to find nothing works. I have checked the APK and Parse is inside it but when I try anything it does not work.
I have tested this with a Test APK with nothing but parse and the test script attached to a button and it also does not work.
using UnityEngine;
using System.Collections;
using Parse;
public class ScriptTest : MonoBehaviour {
public UnityEngine.UI.Text Mytext;
string mytext;
// Use this for initialization
public void MySpecial () {
ParseObject testObject = new ParseObject("TestObject");
testObject["foo"] = "bar";
testObject.SaveAsync();
mytext = "Saved";
Mytext.text = mytext;
Debug.Log (mytext);
}
// Update is called once per frame
void Update () {
}
}
It will not do anything the button will click it will say saved on my text but when I check parse TestObject nothing will be there. Is there something im missing in my build?
I have made sure stripping is also disabled.
This works 100% in editor just not in Android I have no idea or way to test iOS.
I have the exact same problem, using Unity 5.1 and Parse Unity SDK 1.5.1. It works perfectly in the editor but not on Android.
Here's my code :
ParseObject _localPlayer = new ParseObject();
Task query = _localPlayer.SaveAsync();
while (!query.IsCompleted)
yield return null;
if (!query.IsFaulted && !query.IsCanceled)
{
Debug.Log ("Player successfully created!");
}
else
{
Debug.Log("Failed to create player...");
}
The task generated with the SaveAsync() request doesn't seem to end at all (isCompleted is never true) so it becomes an infinite loop, and the actual cloud operation is not performed (no trace on the Parse dashboard). There doesn't seem to be any exception thrown and the device log doesn't show anything.
I'm really stuck at this point and haven't be able to find any solution anywhere on the web so far. :(
Using Parse SDK 1.3.2 "fixes" the issue.
-> https://parse.com/downloads/windows/Parse/1.3.2
Looks like a big fat regression. ;)
Going back to Parse Unity SDK 1.3.2 worked for me.
LogOutAsync() had to be replaced with LogOut(), but that was the only change I had to make in my code.
The solution to this problem is to reimport project settings from their blank project. It will fix any build problems you have.
The bug report, for those who would like to be kept informed:
https://developers.facebook.com/bugs/1623483557935932/
The main Problem is completely different, please skip to the Edit
I have an exception of an unknown type which doesn't even get thrown properly. Following Code provides the Context:
MMDataAccess.InitDemoDB();
MMDataAccess.InitInternalDB();
MMDataAccess.InitMaintDB();
try
{
SQLiteToDBLib sqltdbl = new SQLiteToDBLib();
sqltdbl.WriteToSQLite();
}
catch(Exception ex)
{
string message = ex.Message;
}
These are the very first lines of my first Activity in my app. The first 3 lines belong to my very own implementation of an in-memory database and are behaving nicely. The problem rises with the next two lines inside the try-block. The declaration and initalistation of the sqltdbl variable never happens. The constructor of SQLiteToDBLib looks like this:
public SQLiteToDBLib()
{
msc = new MSConnection();
}
The MSConnection class doesn't even have a constructor (except for the default one of course).
As you can see i've tried to catch any exceptions, but without success. everything i can figure out is, that a exception is thrown because of the debugger going into the catch section while ignoring everything that has to do with "ex". Without breakpoints everything seems fine. Just without the call to WriteToSQLite which should create a .sqlite file on the external Memory.
What can I do to resolve this error? Is there anything i can catch except the default Exception?
Edit:
After some testing with commented code something interresting happened. I could step into commented code. Well not exactly the commented code, but the code that was there before my changes. Visual Studio somehow shows me the things, that are changed in the file, but is compiling the old code. Up to now i tried to rebuild, clean and build the project in various combinations, unload and reload the project, Restart Visual Studio and restart Windows. Nothing has changed so far. I Will now proceed to create a new .cs File With the exact same Code. I'm working with VS 2013 Community
add static constructor to your SQLiteToDBLib class and perform all static objects initialization in it:
static SQLiteToDBLib()
{
// initialize static members here
}
If this doesn't give you a clue, try enabling CLRE exceptions-break in visual-studio:
DEBUG
Exceptions
Check the 'Common Language Runtime Exceptions' option (under the 'Thrown' column)
Press OK
Restart your app and try again