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.
Related
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...
using UnityEngine;
using System.Collections;
public class VariablesAndFunctions : MonoBehaviour
{
int myInt = 5;
}
The full code is here Unity Official Tutorials
What is the purpose of MonoBehaviour
MonoBehaviour is the base class from which every Unity script derives. It offers some life cycle functions that are easier for you to develop your app and game.
A picture is worthy of thousands of words.
Source of the image: https://docs.unity3d.com/uploads/Main/monobehaviour_flowchart.svg
While the following statement is correct,
"MonoBehaviour is the base class from which every Unity script derives" -
I honestly feel it can be misleading to beginners. The phrase - "every Unity script" - being the culprit.
It gives a beginner the notion that all scripts created in unity must extend Monobehaviour. Which is not the case. You can create scripts that house classes that extend the c# base object class. In doing so, your script is then categorised as not a Unity script but nothing stops it from interacting with other Unity scripts and vice versa.
MonoBehaviour is another class that VariablesAndFunctions is inheriting from. This allows the inheritor to use the methods and variables of the other class providing they have the correct access level modifier set.
In the below example Class1 inherits from Base and so can use the protected method Method1
public class Base
{
protected void Method1 { /*...*/ }
}
public class Class1 : Base
{
public void Method2 { Method1(); }
}
Note in this particular example it would be better for Method1 to be marked as abstract or virtual so then Class1 can override it like so:
protected override Method1()
{
//...
base.Method1(); //Call the implementation of Method1 in Base here
//...
}
In particular though MonoBehaviour is described as being:
MonoBehaviour is the base class from which every Unity script derives.
Therefore when doing scripting in unity, you use this base class to better control how things are accessed so you do not need to do it yourself.
Monobehavior is what most of your scripts inherit from,
if you go to the documentation Click here!
you will see a bunch of variables and methods you get from this Inheritance.
such as:
Public Methods
Messages
Properties
Public Methods
Static methods
The most commonly used method (its under message in the documentation but honestly its better to see it as a function) is Update , its the main game loop, the speed at which the update function is called is based on your fps. But the important thing to take away is that if you didn't inherit from monobehavior, you wouldn't have access to this game loop.
Another important function that you get from Monobehavior is Start, which is called once on a script, and it's called after awake, so if you want to set some variables up you can do it here.
The important thing to take is that if you made a simple C# class that inherits from nothing, you wouldn't have access to these methods discussed. Monobehavior gives you access to many functions that help you build your game.
There are other behaviors your scripts can inherit from like ScriptableObject and StateMachineBehaviour, which give you access to other methods, but Monobehavior is the most common behavior your scripts will inherit from.
It's also good to note that whenever you use Monobehavior, it comes with a transform, some other scripts (Scriptable objects) don't come with a transform. The transform is simply a position in your game/scene where the gameobject lies its an x,y,z coordinate with rotation and scale.
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.
I've been trying for hours now, but I have no idea what is wrong.
Normally, when you define a public object/var in Unity C#, it shows up in the inspector or default references where you can drag something to it.
Here's my test code:
using UnityEngine;
using System;
using System.Collections;
[Serializable]
public class modGlobals : MonoBehaviour {
public static GUIStyle defaultMask;
public GUIStyle trollo;
}
Aaaaaand...
I tried several options, wrote the thing in MonoDev as also in Visual Studio, put all my files in an namespace, w and w/o Serializable attribe but.. What could be wrong?
Thanks for help!
Yes you do need to attach it to a gameObject in the scene in order to show the public members in the property inspector, since your script inherits from MonoBehaviour.
This is what I got.
Additionally, you cannot expose static members in the property inspector - even when marked as SerializableAttribute. System.SerializableAttribute is not valid on this declaration type. It is valid on `class, struct, enum, delegate' declarations only.
In relation to your comment about requiring a globally accessible script with objects, you have a couple options, such as
Singleton script attached to gameObject (example), set with DontDestroyOnLoad() so it persists between scene changes.
Static class