Code runs twice for no reason
INTODAN
✭
Hi everyone, I have a simple weapon script attached to my player gameobject which also has a photonView compoment (Not sure if the last part helps). here is the whole class:
Now what you need to focus on is the start function. There is the debug.Log function, and it logs twice in the unity console. It logs "Ref Player(Clone)" twice. can anybody explain to me how to make the start function run only once?
Thank you,
Itai
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using Photon.Pun;
using Photon.Realtime;
namespace Com.MyCompany.MyGame
{
public class playerWeapon : MonoBehaviourPun
{
public GameObject bomb;
private float speed = 300f;
private float up = 30f;
private bool shouldThrow = false;
private GameObject camera;
private bool shoot = false;
// Start is called before the first frame update
void Start()
{
if (photonView.IsMine && PhotonNetwork.IsConnected == true)
{
Debug.Log("ref" + gameObject.name);
camera = Camera.main.gameObject;
}
}
// Update is called once per frame
void LateUpdate()
{
if (!photonView.IsMine && PhotonNetwork.IsConnected == true)
return;
if (Input.GetKeyDown(KeyCode.Q))
{
shouldThrow = true;
Debug.Log("q");
}
/* if (Input.GetKeyDown(KeyCode.Mouse0))
{
}*/
}
private void FixedUpdate()
{
if (!photonView.IsMine && PhotonNetwork.IsConnected == true)
return;
if (shouldThrow)
{
shouldThrow = false;
gameObject.GetComponent<PhotonView>().RPC("createBomb", RpcTarget.AllViaServer);
}
}
[PunRPC]
public void createBomb()
{
GameObject c4 = Instantiate(
bomb,
new Vector3(camera.transform.position.x, camera.transform.position.y, camera.transform.position.z),
Quaternion.Euler(camera.transform.rotation.eulerAngles.x, camera.transform.rotation.eulerAngles.y + 90, camera.transform.rotation.eulerAngles.z)
);
c4.transform.position += camera.transform.forward;
c4.GetComponent<Rigidbody>().AddForce(camera.transform.forward * speed);
c4.GetComponent<Rigidbody>().AddForce(new Vector3(0, up, 0));
}
}
}
Now what you need to focus on is the start function. There is the debug.Log function, and it logs twice in the unity console. It logs "Ref Player(Clone)" twice. can anybody explain to me how to make the start function run only once?
Thank you,
Itai
0
Answers
-
Hi everyone, so the solution was to create a new script, name it differntly, and copy and paste row by row the entrie thing. This is really weried but it works0