I'm having a headache using the State Synchronization support of UNET in Unity3d 5.
According to documentation http://docs.unity3d.com/Manual/UNetStateSync.html
The class should inherit from NetworkBehaviour. Well, I'm doing that way:
class Player : Character
abstract class Character : MovingEntity
abstract class MovingEntity : NetworkBehaviour
Where, in Character I'm storing all player data using the [SyncVar] custom attribute. Like this:
public abstract class Character : MovingEntity
{
[SyncVar]
public string name;
}
But in inspector I'm receiving the following error:
The type or namespace name `SyncVar' could not be found. Are you missing a using directive or an assembly reference?
and
The type or namespace name `SyncVarAttribute' could not be found. Are you missing a using directive or an assembly reference?
Not a experient programmer in C#. The fact of the class being inherited from another class that inherits from NetworkBehaviour doesn't give me support to do that?
Just FYI, adding [SyncVar] on MovingEntity (That direct inherits from NetworkBehaviour) works just fine.
What I'm doing wrong?
There is any good practice in class inheritance that I'm not doing in this example?
Thanks in advance and sorry for the poor english.
You might have forgotten to add the using directive in one of your file:
using UnityEngine.Networking;
This has nothing to do with inheritance. Each c# file need to explicitly state the namespace it is using. Alternatively, you can use the fully qualified names:
[UnityEngine.Networking.SyncVar]
This is not the answer to the main problem in the question, just to help whoever ends up here due coming from google like I did - please don't delete this answer it would have helped me if it was here when I needed it
If you guys ended up in this page because UNet been deprecated and are getting a reference error for NetworkBehaviour, install package:
Multiplayer HLAPI
Seems to have solved the dependency issue in one of the packages I'm using. I don't have multiplayer functionality so I don't know if it is working or not. But at least I can still use the package that has a class that inherits from NetworkBehaviour.
Related
I make multiplayer script and OnStartLocalPlayer method can't override for some reason. Error:
Assets\Sharoidi\Scripts\PlayerController.cs(10,26): error CS0115: 'PlayerController.OnStartLocalPlayer()': no suitable method found to override
Script:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using Unity.Netcode;
using UnityEngine.Audio;
using TMPro;
public class PlayerController : NetworkBehaviour
{
public override void OnStartLocalPlayer() {
Debug.Log("Local Player started!");
}
}
I tried to delete override, but now method doesn't get called when local player spawns.
My guess would be that you're duplicating names of built-in classes and then not getting the results you want because Unity doesn't know which class to use. In your snippet you're defining a class called PlayerController, but that's already the name of a different class and that class doesn't have the OnStartLocalPlayer method.
This shouldn't be giving you this particular error, though, but it makes me wonder about your NetworkBehaviour class. That is also a built-in class, but it's in the UnityEngine.Networking namespace, which you are not using in your snippet. Again, this should give you a different error, that "it's not defined, are you missing an assembly or using statement," but you're not getting that error, which makes me think maybe you have a script somewhere in your project called NetworkBehaviour that's shadowing the built-in.
Try replacing your class declaration with this and see if this does anything different:
public class PlayerController : UnityEngine.Networking.NetworkBehaviour
{
This would point explicitly to the built-in NetworkBehaviour. If this fixes it for you then great, but really stop giving your stuff the same names as built-in things.
I'm pretty new to C#, so I may just not know the terms to search for.
I'm trying to export a subset of classes available in a namespace to manage visibility.
High level - there's an assembly which provides the UnityEngine namespace. UnityEngine is huge. I'm writing various utility libraries which utilize a very small subset of the UnityEngine. A few points:
Including the "wrong" classes in the libraries results in code being untestable (and thus does not get tested).
The main purpose is it's clear to other developers when boundaries are being crossed.
I desire to use the UnityEngine types as opposed to my own so there's not a bunch of conversion. For example Vector2 is a type that should be used everywhere.
So given that, I'm trying to get something like the following to work, though other approaches are welcome too:
... Assembly UnityTypes.asmdef (my code)
... Depends on UnityEngine
namespace UnityTypes {
using Vector2 = UnityEngine.Vector2;
}
... Assembly PathFinding.asmdef (my code)
... Depends On UnityTypes (my code), NOT UnityEngine
namespace PathFinding {
using UnityTypes;
public class GraphBuilder {
public INode AddNode(Vector2 position, List<INode> edges);
}
}
... Assembly TheGame.asmdef (my code)
... Depends on Pathfinding and UnityEngine
namespace TheGame {
using UnityEngine;
public class PathFinder : MonoBehavior {
public void Awake() {
for (var waypoint : GetComponentsInChildren<IWaypoint>) {
_graph.AddChild(/* Vector2 */ waypoint.position, waypoint.destinations);
}
}
}
}
Are you trying to "change" the classes of an existing library?
I do not think that is possible or desirable.
You can inherit from those classes (given they are not marked as sealed) to implement your own behavior.
You can, however, not change existing classes.
It is a bit unclear to me what exactly you are trying to do here.
It sounds a bit like in your first script you want to do
namespace UnityTypes
{
using Vector2 = UnityEngine.Vector2;
}
and now you expect that wherever you do
using UnityTypes;
or even wrapping your new code in
namespace UnityTypes
{
}
the other static using is automatically applied.
This is not the case!
The usings in c# are per script.
An alternative sometimes might be using inheritance like e.g.
namespace UnityTypes
{
public struct GameObject : UnityEngine.GameObject { }
}
however, afaik most of the types you would be interested in will most probably be sealed or structs (like the Vector2).
=> Your users will simply have to use the correct usings and explicit types where necessary.
Now regarding the using: You only need it in those scripts where you explicitly use the type name.
So in your case GraphBuilder explicitly has a signature with Vector2 so here you either need the
using UnityEngine;
or have to explicitly write
UnityEngine.Vector2
and also need according assembly reference.
Then in your PathFinder you wouldn't need the
using UnityEngine;
when only accessing and passing the waypoint.position, but in your case you need the namespace anyway since MonoBehaviour etc are also part of it ;)
You still will need the assembly reference as soon as you access any member of a type that is defined in it, even if you only pass it on to somewhere else, you don't need to do that in case you only pass on the type which contain fields and properties etc where the type is used.
I don't know why but when I add my code in some object it gives this error, could someone help me?
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class Room : MonoBehaviour
{
void OnTriggerStay2D (Collider2D other){
if (other.CompareTag("Player")){
CameraController.instance.SetPosition(new Vector2(transform.position.x, transform.position.y));
}
}
}
The generic reason for this is when you have a script file that is named different the class name. Ensure that your c# script file matches the name of your class. i.e. both should be Room.
I had the same problem in Unity, when I tried to attach a script to an object.
After trying/isolating a lot of "google solutions" that didn't help in my case, like checking the name of the script and the class. (I already encountered that when I renamed a script earlier :P)
So to refocus, I first fixed an issue in another script(which had a typo), for another model in the same project.
After that the first thing I did was simply drag the script to the model again.. and it worked...
I'll add this option to my list of options when I'll encouter this “The script don't inherit a native class that can manage a script.” again...
I'm new and I don't know other way to explain so I posted my screenshot of project! Please help me to fix these errors... SCREENSHOT
You seem to be having a lot of problems with references and namespaces use.
First of all, you do not have a Card class defined. You only have a CardModel. Replace Card for CardModel and you will be good to go. Also, it seems you do not have a namespace declared on your class. Declare a namespace so you can use other classes in the same namespace (tipically the project name).
Second, if you are trying to use clases in another folder, you probably have to add the reference with the using keyword.
You're missing probably several using directives. Every class you write should be inside a 'namespace' You declare it after your using directives but before you start writing your classes, like this:
namespace WebShop.CardModel {
public class CardModel {
public string InsertCard(Card card){
And when you are working in the cardModel, unless Card is defined in the same namespace, you need:
using WebShop.Card;
Or whatever namespace you put Card in, that's what is throwing probably 99% of your errors, it is definitely the cause of all but one of the ones in the errors we can see in your screenshot.
I have the following two files:
IGlobalApiProvider.cs
using System.Collections.Generic;
using Vert.Slack;
namespace Vert.Interfaces
{
public interface IGlobalApiProvider
{
List<Im> ImList();
}
}
And the corresponding implementation: SlackApi.cs
using System.Collections.Generic;
using Vert.Interfaces;
namespace Vert.Slack
{
public class SlackApi : IGlobalApiProvider
{
public List<Im> ImList()
{
...
}
}
}
Now, Intellisense is telling me that when I use IM in IGlobalApiProvider it's resolving to Im, which is defined in a file named RtmStart.cs which has no namespace declaration. When I use IM in SlackApi.cs, it's resolving to Vert.Slack.Im which is defined in the Vert.Slack namespace in a file named Im.cs. The weird behavior alerted me to the redundant definition, so I removed it and things are working fine.
However, I'm confused about why Visual Studio behaved differently in these two ways. I can tell something was scanning for the class names in a different pattern in the two situations. I can also tell that being used in the same namespace vs being used in a class that uses the namespace seems to be the trigger. What I don't know is what mechanism controls the logic behind this behavior.
Can anyone shed light on this?
Everything you see is contained in Vert.dll, which consists of one project, Vert.csproj
Link to the four files mentioned in this post as they existed at the time of writing.
This has to do with the difference between the global and Vert.Slack namespaces.
The compiler looks for the most explicit namespace with the proper class defined.
In this example, when the compiler looks for the definition of Im in IGlobalInterfaceProvider.cs, there is no namespace defined (or used) in this file that contains the class, but Im is also defined in this file - which is declared in the global namespace.
When the compiler looks for the definition of Im in SlackApi.cs, Im is found in the explicit Vert.Slack namespace, and utilizes that class.
The answer here is a similar topic and may provide more insight.
This may be related to the fact that your namespaces are in the wrong place ;-)
http://www.stylecop.com/docs/SA1200.html
This answer here gives a good explanation: Should 'using' statements be inside or outside the namespace?