- Home /
Is there something like "destroy otomatically" ? Because I don't destroy but it gets destroyed.
Hi. I'm talking about script. I define a gameobject like
public class Server : MonoBehaviour
{
private GameObject go1;
I don't use anything like Destroy or DestroyImmediate. Is there something I don't know? Because I really don't understand why it gets destroyed..
Answer by pmerilainen · Jul 10, 2019 at 01:05 PM
Have you assigned a value to go1. By default all references are null.
Of course I did. I'm making a multiplayer game. The problem is: I can reach it when first player joins in but I can't reach it when second player joins in.
Here is some parts of my server script:
Browsed through your code snipped. Saw nothing wrong (except the Transform tr; can be local in the OnInco$$anonymous$$gData callback). It seems you do not validate the data at all, so if your data does not have delimiter character | or second entry is not numeric value, the script will not function correctly. So first you should check the data is correct for the second 'player spawn' message (you can log it for instance)
No. There is nothing wrong with delimiter or numeric value.
Let me explain more.
I made the multiplayer game. Two player gets connected. They choose the hero they want (from 4 different heros). And game starts. They can move and they can see each other moving. So there was no problem.
But later, I've learned that I should keep the transforms of the players in Server. Otherwise my game could have been hacked. So I decided to keep the transforms in server. And when a player wants to move, the transform info at the server will change. I will send this changed value to the client.
I think this would be enough for security. But somehow I can't avoid transform from being destroyed. Someone adviced me to use Vector3 ins$$anonymous$$d of Transform. I will try it.
Can you add code here, expecting people to download files to help is asking too much! and will turn people like myself away.
Here is the code I've uploaded:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class SS : $$anonymous$$onoBehaviour
{
private List<Transform> transforms;
Transform tr;
public void Init()
{
DontDestroyOnLoad(gameObject);
transforms = new List<Transform>();
}
private void OnInco$$anonymous$$gData(ServerClient c, string data)
{
string[] aData = data.Split('|');
switch (aData[0])
{
case "CNU$$anonymous$$":
c.number = int.Parse(aData[1]);
tr = new GameObject().transform;
if (c.number % 2 == 1)
tr.SetPositionAndRotation(new Vector3(0, 0, -7), Quaternion.identity);
else
tr.SetPositionAndRotation(new Vector3(0, 0, 7), Quaternion.identity);
transforms.Add(tr);
Debug.Log("SAYI: " + transforms.Count);
if (c.number == 2)
Debug.Log(transforms[1].position.x);
Debug.Log(transforms[0].position.x);
break;
default:
break;
}
}
}
Actually, first thing I need to learn is what should I keep at server. I mean, should I keep whole gameobject or transform or position and rotation ? Which is better ?