I am using LuaInterface with C#, and have got everything set up correctly.
What I want to be able to do is that when the script is started using lua.DoFile(), that the script has access to a Player object that I can send...
Current Code:
public static void RunQuest(string LuaScriptPath, QPlayer Player)
{
QMain.lua.DoFile(LuaScriptPath);
}
But as you can see the script will not have access to the Player object.
I see two options. The first one is to make your player a global variable for Lua:
QMain.lua['player'] = Player
Then you'll be able to access player in your script
The second option is to have the script define a function accepting player as parameter. So if your current script contains ...code... now it will contain:
function RunQuest(player)
...code...
end
and your C# code will look something like this:
public static void RunQuest(string LuaScriptPath, QPlayer Player)
{
QMain.lua.DoFile(LuaScriptPath); // this will not actually run anything, just define a function
QMain.lua.GetFunction('RunQuest').Call(player);
}
Related
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.
Working on getting familiar with C# and unity development. Today I am working on getting a reference to a Text UI object in my script. The following code below produces this error:
NullReferenceException: Object reference not set to an instance of an object
handle.Awake () (at Assets/handle.cs:20)
Script looks like this:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using playPORTAL.Profile;
using UnityEngine.UI;
public class handle : MonoBehaviour
{
public Text myText;
// Start is called before the first frame update
void Start()
{
}
void Awake()
{
myText.text = "#organickoala718" ;
}
// Update is called once per frame
void Update()
{
}
}
What needs improving to correctly get a reference to the Text UI element?
In general: The same way as with any other Component.
Either reference it via the Inspector or use GetComponent(Tutorial) or one of its variations.
So if that Text Component is attached to the same GameObject as your script then you could use GetComponent(API) to get the reference on runtime
private void Awake ()
{
if(!myText) myText = GetComponent<Text>();
myText.text = "#organickoala718" ;
}
Also checkout Controlling GameObjects with Components
Btw you should remove the empty methods Start and Update entirely. They are called by the Unity Engine as messages if they exist so the don't need to exist and only cause unnecessary overhead.
You need to either set the myText value of your handle instance from another script, or set it in the Unity Editor's Inspector window, when you've selected a GameObject that has your handle component added.
You need to drag the object you are referencing to from the Unity editor, in the scene, to the script itself.
First attach the script you made to a GameObject in the Unity Scene.
Then drag the "text" component to the Script you recently attached to the GameObject
That would solve the problem you have.
Another approach would be to declare a
public GameObject UITextElement;
Instead of a public text as you did.
Do the same as i wrote before and in the script write:
UITextElement.GetComponent().text = "Write your text here!";
i am creating all new unity project and added Empty GameObject in to my first Scene, and also created and added script to that object, but when i'm trying to run my scene script dose not showing any output in my console window.
here is my code
using System.Collections;
using UnityEngine;
public class ScriptObject : MonoBehaviour
{
// Start is called before the first frame update
void Start()
{
Debug.Log("Start method called.");
}
// Update is called once per frame
void Update()
{
Debug.Log("Update method calling.");
}
}
You need to turn on showing of another types of log messages ("Info" messages in your case).
You need to be sure that your button of the console output is enabled. Just try to click on the button.Look at the screenshot
I am adding the following line of code in my script to enable controller support for unity VR game which is being played in google daydream. Without this code, the controller is not showing in the final build of the game
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using System;
public class InputManager : MonoBehaviour
{
public GameObject controllerMain;
public GameObject controllerPointer;
private void Start()
{
controllerMain.SetActive(true);
controllerPointer.SetActive(true);
GvrPointerInputModule.Pointer =
controllerPointer.GetComponentInChildren<GvrLaserPointer>();//3
}
}
Can someone explain me why I need to enable controllermain and controller during gamelay they are not disabled in the hierarchy.
And explain the 3rd line of code
The controllerMain and controllerPointer variables are just references to GvrControllerMain and GvrControllerPointer GameObject prefabs respectively. GvrControllerPointer provides built-in laser pointer used for raycasting. GvrControllerMain is required to use the Daydream controller.
Calling controllerMain.SetActive(true) will activate GvrControllerMain. The-same thing applies to GvrControllerPointer when you call call SetActive on it. Both of these should be activated when running daydream. This is why the activating and de-activating of them happens during run-time because that's when you can determine if this is a daydream device or not.
GvrPointerInputModule.Pointer is used to set the pointer type. There is a GvrLaserPointer and GvrReticlePointer. In your example, you were telling the Gvr module to use GvrLaserPointer and is a laser visual that helps users locate their cursor when its not directly in their field of view.
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");
}