- Home /
Instantiate then access component(s)
Hey guy's I'm using Photon to instantiate my player. (IT WORKS)
But anyone who uses photon knows that the camera and movement controls need to be disabled first then re-enabled.
Well, the way my player is set up, none of the things are directly childed to the Main parent.
The way my player is set up is like this.
Player
FirstPersonControls
player
Capsule
Main Camera
RightTouchPad
LeftTouchPad
What I need to do is enable the Joysticks for both touch pads. (both scripts on each one are named Joystick)
And re-enable the Main Camera
from this code below.
// we're in a room. spawn a character for the local player. it gets synced by using PhotonNetwork.Instantiate
GameObject myPlayerGO = (GameObject)PhotonNetwork.Instantiate("Player", Vector3.zero, Quaternion.identity, 0);
Well when I try to FIND the camera with this code below
myPlayerGO.transform.Find ("Player/FirstPersonControls/player/Main Camera").gameObject.SetActive (true);
I get this error --- NullReferenceException: Object reference not set to an instance of an object IngameNetManager.Awake () (at Assets/_Imported/_Scripts/_Network/IngameNetManager.cs:21)
I've tried it with FindChild as well, same outcome.
And for the Joysticks, I don't understand how to access the "Joystick" scripts from them. and re-enable them.
If it was all done from one object, then it was be easier to do this, but I've never delt with doing things with child objects.
Can you please help me?
Answer by N1warhead · Nov 08, 2014 at 01:40 PM
Hey I got it working, I'm putting it in Answer so anyone looking can understand and know this is the answer.
By the way Linus (You're ways are correct) for Single player stuff.
But with Photon this is how I had to do it... (Just a tad bit different) than you were pointing out.
// THIS SCRIPT DEALS WITH NETWORKING PLAYER
// ALSO ENABLED LOCALIZED THINGS SUCH AS CAMERA, Joysticks, etc.
// VARIABLES FOR CAMERA AND THE JOYSTICKS
public Transform myCam;
public Transform LeftJoy;
public Transform RightJoy;
// Use this for initialization
void Start () {
// IF THE PLAYER IS MINE, ENABLED THESE OBJECTS.
if (photonView.isMine) {
myCam.transform.gameObject.SetActive(true);
LeftJoy.transform.gameObject.SetActive(true);
RightJoy.transform.gameObject.SetActive(true);
}
}
Yes its the exact thing I tried to explain, just different ways of thinking about it. Store the reference somewhere so you can find it again even if its disabled.
Answer by Linus · Nov 08, 2014 at 08:45 AM
.Find does not find diabled gameobjects/transforms. You need to make a reference before disabling it. Or you can save the reference as part of the prefab in the editor.
Not tested with photon just did a quick test in editor.
//Assign the gamera transform in the editor, and save it as part of prefab
using UnityEngine;
using System.Collections;
public class photonttest : MonoBehaviour {
public Transform cameraTransform;
// Use this for initialization
void Start () {
cameraTransform.gameObject.SetActive(true);
}
}
That would work, (I've tried this as well), and I appreciate your effort.
But sense I have to (Instantiate) the player it doesn't recognize it exist.
Because this script isn't on the player, it's on the In game Network manager.
So when i attach $$anonymous$$G. - The Camera, and erase the prefab from the scene after doing changes (so i can spawn player) the Camera transform slot changes to "$$anonymous$$issing" so it doesn't recognize it anymore once removed from the scene.
I would just add the Camera from the prefab (from Project folders) but when I go to see the children of the the parent it stops at FirstPersonControls and doesn't show anything else after that.
But you could you not access from another place.
Like:
myPlayerGO.GetComponent<photonttest>().cameraTransform.gameObject.SetActive(true);
When I assign the camera transform in the scene, save the prefab, delete the one in scene and drag in anew one it still remembers the assigned transform as long as its a a child transform at least.
Another way could also be to have the camera + (other componets that cant be on at spawn) component disabled on spawn, but have the transform enabled so it can be found.
Your answer
Follow this Question
Related Questions
Multiple Cars not working 1 Answer
Help Understanding the Use of Unity with Android 1 Answer
Android Native Plugin (NOT WORKING) 1 Answer
Android Require Wifi to play? 1 Answer
Unity3d Controls Not Working Win8 0 Answers