Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by N1warhead · Nov 08, 2014 at 08:17 AM · androidphoton

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?

Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

2 Replies

· Add your reply
  • Sort: 
avatar image
0
Best Answer

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);
 
             } 
     }
Comment
Add comment · Show 1 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Linus · Nov 09, 2014 at 09:11 AM 0
Share

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.

avatar image
1

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);    
     }
     
 
 }


Comment
Add comment · Show 2 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image N1warhead · Nov 08, 2014 at 09:00 AM 0
Share

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.

avatar image Linus · Nov 08, 2014 at 09:17 AM 0
Share

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

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

27 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

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


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges