Passing a boolean between different objects not working - c#

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");
}

Related

CharacterMovement::CmdFight() called on NetworkCharacterContainer without authority error

I have been creating a multiplayer game using Mirror. I have coded an entire Fight function, and I turned it to a command function since i want it to be run on a server (because I think it's easier to calculate who you shot on server then on a client), but I get an error:
Command Function System.Void CharacterMovement::CmdFight() called on NetworkCharacterContainer(Clone) without authority
(The error on the title appears on host and I don't know why).
Here is the code:
public void HandleFighting()
{
if(Input.GetMouseButtonDown(0))
{
Debug.Log(this);
CmdFight(); //! We want to fight!
}
}
[Command]
private void CmdFight()
{
if (!isLocalPlayer)
{
Debug.LogWarning("Command Function CmdFight() called on CharacterMovement without authority.");
return;
}
//! If we clicked on left mouse button
if (WeaponData != null)
{
RaycastHit raycastHit;
if (Physics.Raycast(CharacterPosition, Input.mousePosition, out raycastHit, WeaponData.Range))
{
//!We hit a player
GameObject ho = raycastHit.collider.gameObject;
Health health;
if (ho.TryGetComponent<Health>(out health))
{
//! We hit a player
health.SubstactHealth(WeaponData.damage);
health.AddEffects(WeaponData.effects);
}
}
}
}
I am spawning player normally and on network server (like this):
public override void OnServerAddPlayer(NetworkConnectionToClient conn)
{
//TODO Read the code and fix it
GameObject Player = Instantiate(playerPrefab,transform.position,transform.rotation); //! create a player
NetworkServer.Spawn(Player);
If anybody can help, please write it down below.
I have asked ChatGP and it told me like 7 steps, i did them all and nothing worked.
Some of these steps were:
Make sure that the object that is calling the CmdFight() function has a NetworkIdentity component. You can add it by clicking on the object in the Unity editor, and then in the "Add Component" menu, searching for "Network Identity".
or
Make sure that the NetworkIdentity component is correctly set up. In the inspector, check that the "Local Player Authority" option is enabled. This option allows the object to execute command functions as a local player object.
or even
Make sure that the object was instantiated using the NetworkServer.Spawn() method or a NetworkIdentity component with the Local Player Authority option selected.

This Unity Scene is not loading

I don't know why my scene is not loading, can I get help?
When I press the button nothing happens.
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.SceneManagement;
using UnityEngine.UI;
public class gamecontroller : MonoBehaviour
{
public Text points;
public void Start() {
points.text = Score.scoreval + " POINTS";
}
public void Restart() {
SceneManager.LoadScene("SampleScene");
Score.scoreval = 0;
}
public void Menu()
{
SceneManager.LoadScene("Menu");
Score.scoreval = 0;
}
}
Reason might be that you did not assign the scene in your build. This is an easy fix if this is the case. To check if it is, you can go to the Build Settings... under the File tab. Once in there, you should see the Scenes In Build category, in there you should also see your scene(s). If not, you will need to assign them by going to each scene and pressing Add Open Scenes. Once done, it should work properly from there.
Not sure if this is an issue, it has been a while since I have coded in C#. But, in my scene manager, I wrote the scenes name as such:
public void StartButton()
{
SceneManager.LoadScene(labScene);
}
Not sure if that matters, but I thought I might as well put that out there.
There seems to be a bug from Unity side as it has happened with me sometimes that whenever I'm trying to load the scene using the LoadScene method by passing the SceneName as the parameter, Unity throws up an error even when those scenes have been added in the Build Settings.
Removing the scenes from the build settings and adding them again is what worked for me. :)

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)

RPC crashing Unity

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]);
}

Unity Designers may only attach my script to a terrain object

Is there a way I can prevent the designer from attaching my script to anything except a terrain type?
Normally when I create a script/Component/MonoBehavior, Unity allows a designer to add it to any game object. How do I limit that?
Two methods come to mind, but none are very elegant.
1) Implement OnValidate(). The downside is that's it's called only when modifying component's values, or entering/exiting game mode.
void OnValidate() {
if (GetComponent<Terrain>() == null) {
Debug.LogError("You can't attach this component without terrain!");
DestroyImmediate(this);
}
}
2) Make the script run in the editor, and implement OnAwake(). But be aware that methods like Update() will be called in the editor.
[ExecuteInEditMode]
class MyScript: MonoBehaviour {
void OnAwake() {
if (GetComponent<Terrain>() == null) {
Debug.LogError("You can't attach this component without terrain!");
DestroyImmediate(this);
}
}
}

Categories

Resources