I have been struggling with this for about a week now. I know I am missing something but I have no idea how to fix this.
using UnityEngine;
using System.Collections;
using System.Collections.Generic;
public class otherPlayers : MonoBehaviour {
public Transform Ship;
private GameObject extUser;
private bool created = false;
private Vector3 newPosition;
public float smooth;
// Use this for initialization
void Awake () {
newPosition = transform.position;
}
// Update is called once per frame
void Update () {
}
[RPC]
void ServerUpdate(string ServerGuid, float ServerX, float ServerY, float ServerZ){
if (created == false){
extUser = Instantiate(Ship, new Vector3(ServerX, ServerY, ServerZ), Quaternion.identity) as GameObject ;
created = true;
Debug.Log (extUser);
}else{
Debug.Log("Server data"+":"+ServerX+":"+ServerY+":"+ServerZ);
Vector3 newPosition = new Vector3(ServerX, ServerY, ServerZ);
extUser.transform.position = Vector3.Lerp(transform.position,newPosition, smooth * Time.deltaTime);
}
}
}
Line 30 is generating the error. How do I get it to update extUser that was created on line 24? I know 24 is working because I see the ship that was created by the server. It appears where it is supposed to but I can’t get it to update when the object moves. The server is sending out the updated info I can see it in the debug log (line 28 ).
The reason i can see, is that you instaniate in the if condition only, so if the if condition is false, then extUser will be null. Quite simple to fix, basically you need to either instaniate the extUser or remove that line. I mean you can change that if statement also to something like this
No I am not destroying anything yet. I want to get the stuff in and moving before I start deleting it.
. I did try putting
at line 18. I got 35 errors and then they stopped. Maybe it is a timing thing. However after the errors stopped the other ship that is Instantiated never moved even though the server sent update coordinates.
I think the problem is that Ship is of type Transform and extUser is of type GameObject. When you instantiate Ship and cast it as a GameObject the result is null because Transform cannot be converted to GameObject. Try something like:
Transform shipInstance = Instantiate(Ship, new Vector3(ServerX, ServerY, ServerZ), Quaternion.identity) as Transform;
extUser = shipInstance.gameObject;