Player does not teleport when using PUN2 network. Basically, I do not know how to add the function to get the player to teleport while being on a network. I am only just getting into GameDev and I am trying to learn as much as possible while creating a game at the same time. So, I wanted to add portals to teleport the players to and from different parts of the map, but it doesn't work while being logged into a network unfortunately. Thank you for any assitance in solving this issue its been bothering me for a few days now lol. Here is the c# script that I use to teleport the player and again remember I know it needs to be using some type of network code inside I just do not know how to add it unfortunately. Thanks!!
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class Teleport : MonoBehaviour
{
public Transform teleportTarget;
public GameObject thePlayer;
void OnTriggerEnter(Collider other)
{
thePlayer.transform.position = teleportTarget.transform.position;
}
}
I'm not sure if this has anything to do with your issue, but when using Pun2, you probably want to include it
using Photon.Pun;
using Photon.Realtime;
and then you may need to check which player has hit the trigger. I hope this works, but please provide a few more details on what exactly isn't working.
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 created a script in Unity (using Visual Studio Code). Here is my code:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class Propellermovement : MonoBehaviour
{
private GameObject propeller;
public float Propellerspeed;
void Update()
{
transform.Rotate(Vector3.forward * Propellerspeed * Time.deltaTime);
}}
I don't know if someone knows how to help me.
I created new scripts and it still didn't work. I can't add the component to an object, and I don't know if it is because it's an object into an object (sorry English its not my first language).
As noted in the comments, your file name must exactly match the name of your class. You need to have your file named exactly "Propellermovement.cs". If you change the name of your class, you must also change the name of your CS file in order for Unity to properly recognize it.
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 trying to write a little mod for Kerbal Space Program a game which uses Unity. I've got a class that is a child of MonoBehaviour which loads correctly and all. Part of this mod involves creating a new light source in the current scene. My question is as follows: is it possible for me to create a new Unity light source in the current scene using a script rather then the unity engine scene editor (which I obviously don't have access too as a modder).
Example of the sort of thing I'm looking for (I know it won't actually look anything like this but just to give you an idea of what I need)
UnityEngine.getCurrentScene().createObject(new Light(pos, direction, color, strength));
Create the gameobject and add the light component:
using UnityEngine;
using System.Collections;
public class ExampleClass : MonoBehaviour {
void Start() {
GameObject lightGameObject = new GameObject("The Light");
Light lightComp = lightGameObject.AddComponent<Light>();
lightComp.color = Color.blue;
lightGameObject.transform.position = new Vector3(0, 5, 0);
}
}
I'm currently working on an inventory system in Unity3D, and came upon a weird problem. I have created non-MonoBehaviour classes for my inventory system (in case that matters), so that I have an Inventory class which holds a list of Slot objects, which in turn holds a list of Item objects.
Then I added a component script to my "HudInventoryPanel", called "HudInventoryController", which looks like this:
using UnityEngine;
using System.Collections;
public class HudSlotController : MonoBehaviour {
private InventoryController ic;
// Use this for initialization
void Start () {
ic = GetComponent<InventoryController>();
}
// Update is called once per frame
void Update () {
}
}
However, inside the Start() method, the InventoryController (part of my Player) hasn't been created yet, and it seems to me like the gameobjects are created in alphabetical order...?
How should I deal with this problem?
You can specify script execution order in the project settings (Edit -> Project settings -> Script execution order), so use that to make sure your scripts are executed in the correct order. In addition to that, you can use the Awake() method as all Awakes are executed before any Start() method. Hope a combination of these helps you!
I solved this by creating an intermediate variable in my InventoryController script, plus creating a "manual" getter; https://gist.github.com/toreau/f7110f0eb266c3c12f1b
Not sure why I have to do it this way, though.