RPC crashing Unity - c#

I'm experiencing a certainly disturbing issue in the last hours.
It appears that every RPC call I do using an array (int, float, byte, etc) is crashing the whole engine.
I've made a little working example of how to reproduce, and I would like to know if it's certainly a bug, or if im doing something very wrong:
using UnityEngine;
using System.Collections;
public class NewBehaviourScript : MonoBehaviour {
// Use this for initialization
void Start () {
Network.InitializeServer(4, 25000, true);
networkView.RPC("test", RPCMode.All, new float[5]);
}
// Update is called once per frame
void Update () {
}
[RPC]
void test(float[] lol){
Debug.Log("received "+lol);
}
}
Using only this script in a Camera with a network view is enough to get the thing crashed. Did I do something wrong??
Thanks in advance!

It is a bug in 4.6.3 and 4.6.4 when i try to send a bytearray over an rpc in unity5 it does the job. Hope it helped

Using RPC call in Unity is restricted to certain types of parameters:
You can use the following variable types as parameters to RPCs:-
int float string NetworkPlayer NetworkViewID Vector3 Quaternion
So what you can do instead is serialize/convert the array to a string, and then back to array on the other side.

You need to add OnServerInitialized function to your class. And after that you can use networkView.RPC("test", RPCMode.All, new float[5]); in this function.
And also i can't see NetworkView instance in your class.
Like
public NetworkView networkView;
void OnServerInitialized(){
Debug.Log("Server initialized and ready");
networkView.RPC("test", RPCMode.All, new float[5]);
}

Related

I'm having an error in unity for a script that changes the scene to another scene

This is an image of the errors
This is the script
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.SceneManager;
public class LevelComplete : MonoBehaviour
{
public void LoadNextLevel ()
{
SceneManager.LoadScene(SceneManager.GetActiveScene().BuildIndex + 1);
}
}
Please help me with the errors
I tried to debug the error by checking the script and UI animations that connect to the event that changes the scene. I also tried using SceneManager.LoadScene (name); but it did not work.
You have two issues here. First of all, you declared the class twice. It might be twice in the same file, or in different files. Second of all, you need to use UnityEngine.SceneManagement instead of SceneManager.
The simplest solution is to check for double declarations, and for the using, replace
using UnityEngine.SceneManager; with using UnityEngine.SceneManagement.

Unity giving compile error without any error

I am learning unity 3d game development and I was following a tutorial and wrote this code for a ball to apply force on its X-axis, but unity said all compile errors must be resolved before going in Playmode:
code:
using System.Numerics;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class AddConstantForce : 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,0,0);
}
}
I have update unity version. tried various new sample scenes, and after this error pops it also gives the error with default empty void start and void update functions too.
please help
Without the error information, this is somewhat difficult to debug but I do see two errors with your code.
First, you have a typo on your Update function you are calling GetComponent<RigidBody> when the actual name of the class is Rigidbody see Unity's documentation for reference https://docs.unity3d.com/ScriptReference/Rigidbody-velocity.html
There is also an ambiguous reference between Vector3 as both Systems.Numerics & UnityEngine defines that type. you want to stick with the UnityEngine definition. so just delete the first line.
So in the end this code should be working without compilation errors
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class AddConstantForce : 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,0,0);
}
}
Try define the getcomponet variable in the void update and then make a new line with the varible first then velocity thing or/and use += insted of =.

Getting the error "The name 'ObjectPoolingManager' does not exist in the current context"

I am following a tutorial on how to make a gun to shoot a bullet, but I am getting this error:
"The name 'ObjectPoolingManager' does not exist in the current
context"
I am following the tutorial exactly as is, and I don't know why this happens.
Here is my code:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class PL : MonoBehaviour {
public GameObject bulletPrefab;
public Camera playerCamera;
// Update is called once per frame
void Update() {
if (Input.GetMouseButtonDown(0)) {
GameObject bulletObject = ObjectPoolingManager.Instance.GetBullet(true);
bulletObject.transform.position = playerCamera.transform.position + playerCamera.transform.forward;
bulletObject.transform.forward = playerCamera.transform.forward;
}
}
}
I wouldn't worry too much about object pooling until you're comfortable with the basics of Unity and C#. It is an optimization pattern. I would just GameObject.Instantiate(yourBulletPrefab) and GameObject.Destroy(yourBulletGameobject) instead until performance becomes an issue.
Unity doesn't have built-in object pooling. You have to create yourself. Tutorial which you followed forgot to add it.

Unity MLAPI ServerRPC not getting called

I'm implementing a system in which a player can pickup an item from the game world. After picking the item up, I want to destroy the game object. To do this, I figured I'd make a ServerRPC that deletes the object, but the ServerRPC is not getting called. I tried making a ClientRPC as well, but that one isn't being called either. I noticed that it only gets called when the host of the game tries to pick up the item -- any ideas on how to fix this? The documentation is pretty bare but please let me know if I missed anything. Here is my code:
public override void Interact(GameObject player)
{
player.GetComponent<PlayerInventory>().AddItem(this.item);
Debug.Log("before");
TestClientRpc();
TestServerRpc();
}
[ServerRpc]
public void TestServerRpc()
{
Debug.Log("server");
// Destroy(gameObject);
}
[ClientRpc]
public void TestClientRpc()
{
Debug.Log("client");
// Destroy(gameObject);
}
EDIT: image of the components on the weapon
EDIT: This class extends a class that extends the networkbehaviour class. The objects are just sitting in the scene at start -- they are not spawned/instantiated (not sure if that matters or not).
The most plausible solution to this problem , either it is running into a error in some previous lines or as some other person pointed out , requireOwnership is not set to false .
For in depth analysis of MLAPI , https://youtube.com/channel/UCBStHvqSDEF751f0CWd3-Pg
I found the tutorial series on this channel best
I needed to add the following attribute to my ServerRpc:
[ServerRpc(RequireOwnership = false)]
since the player was not the owner of the item.
I have the same issue. Been looking at it for 2 days but I am inclined to think it is a bug to be honest.
I just spent 4+ hours banging my head against the wall because they didn't write this important info in the documentation. When calling ServerRpc from a client that doesn't own the Network Object, RPC attribute should be set to RequireOwnership = false.
For me the issue was that the GameObject(with NetwrokObject component) was sitting in the scene before starting the game, I couldn't use NetworkVariables nor ServerRpc. I think those are only usable on spawned object, but not sure. So I would say : either instantiate the object and add it to the NetworkManager or use normal variable on those objects.
Had the same issue but later realized I just forgot to inherit NetworkBehaviour
For Example:
using Unity.Netcode;
using UnityEngine;
class myObject : NetworkBehaviour {
public override void Interact(GameObject player)
{
player.GetComponent<PlayerInventory>().AddItem(this.item);
Debug.Log("before");
TestClientRpc();
TestServerRpc();
}
[ServerRpc]
public void TestServerRpc()
{
Debug.Log("server");
//Destroy(gameObject);
}
[ClientRpc]
public void TestClientRpc()
{
Debug.Log("client");
// Destroy(gameObject);
}
}
I had the same problem, where the ServerRpc wouldn't even be called. For me it worked after I made the GameObject that my script was attached to a prefab and added it to the NetworkManager's list of NetworkPrefabs.
Note: (I am using Netcode for GameObjects, I don't think it will be the same for other Multiplayer solutions)

Passing a boolean between different objects not working

I know this has been covered in the past, but I believe this issue is being caused by the update to Unity 5, thus deprecating the older information. No errors come up, but the debug won't come up. The public variable of 'pushBlockEnter' does become true visibly on the player script, but the pushBlock class will not recognize it no matter what. I just want to be able to check if a variable is true in one script from another script in Unity 5, any way of doing it would be fine. I'm sure it is something simple but I just can't figure it out...I should also mention that both scripts are on a different object. Thanks in advance!:
public class pushBlock : MonoBehaviour {
public Player playerScript;
void Update () {
if (GameObject.Find("Player").GetComponent<Player>().pushBlockEnter)
{
Debug.Log ("do something blahh");
//Do Anything
}
}
}
Figured it out:
GameObject playerReference = GameObject.Find("Player");
void Update(){
if (GameObject.Find("pushBlockCollider").GetComponent<pushBlockCollider>().inPushBlock){
Debug.Log ("got to pushblockCollider True");
}

Categories

Resources