Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 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 HattyCastles · Jul 16, 2017 at 04:07 PM · scripting problemnullreferenceexceptiongetcomponent

How can I access another script in my prefab to set a variable after my network player setup runs?

Hello all. I've run into a problem that seems like it should be super easy to solve, but that I've been stuck on for longer than I'd like to admit. I'm spawning a player on my network and running a player setup script that inherits from NetworkBehavior. In that script, I instantiate a camera prefab and set its target to be the local player that the network has just created. Then, I have a separate player control script that the player setup script disables for non-local players.

The issue is that, for local players, my player controller script needs a reference to the camera that is pointing at the player. It uses this direction for camera relative movement. It seems like there should be many ways to do this, but I have run into walls with all of them. I wanted to post to help me understand why I can't do the things I'm trying to do and to find a solution.

First I tried to simply created a public var with the type ControllerScript and drag the controller script in the prefab to that reference, but the editor wouldn't let me do this. Can you not use public vars to access user defined types in the same prefab? Then, I tried to access the ControllerScript via GetComponent() in the Start() method on my player setup script. This call always comes back null, though. Do all of the scripts not exist when start Start() is called? Finally, I thought I would just create the camera in player setup's Start(), save its reference, and access that in PlayerController with some method that runs after Start(). I can't find a method that always runs after Start() and before Update(), though. I also can't move the camera creation in player setup to Awake() because isLocalPlayer isn't set in Awake().

I feel that there is an easy solution, but I might be misunderstanding something and unable to see it. I've attached my player setup script here. All of these scripts are on a player prefab that the network is spawning. Any help is greatly appreciated.

public class PlayerSetup : NetworkBehaviour {

 public Behaviour[] toDisable;
 public GameObject camPrefab;

 // Holds reference to camera after creation
 public GameObject camRef;
 public ThirdPersonUserControl tpc;



 void Start () {
 
     // Turn off move scripts for all other players
     if (isLocalPlayer) {
         // Create camera and set it to follow this player
         camRef = Instantiate (camPrefab, transform.position, transform.rotation) as GameObject;
         camRef.GetComponent<CameraFollow> ().target = transform;

    // Whether I have this line or not, tpc ends up null
         GetComponent<ThirdPersonUserControl> ().m_Cam = camRef.transform;

         tpc = transform.GetComponent<ThirdPersonUserControl>();
         if (tpc != null) {
            // This line throws NullReferenceException without the if statement
             tpc.AssignCamera (camRef.transform);
         } else {
             Debug.Log ("controller ref never assigned");
         }
     } else {
         Debug.Log ("Not Local Player");
         for (int i = 0; i < toDisable.Length; ++i) {
             toDisable [i].enabled = false;
         }
     } 

 }
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

1 Reply

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

Answer by HattyCastles · Jul 16, 2017 at 07:56 PM

So I figured out what was going on, and it had nothing to do with my question. I had two copies of a script with the same name in my project. Part of my prefab was referencing one from very early development. The behavior looked the same, but the classes that seemed like they should be the same weren't actually compatible.

Lesson for me to be more careful managing my files. Heads up to everyone that Unity won't complain about two classes with the same name if they are in different directories.

Comment
Add comment · 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

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

107 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 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 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 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

GetComponent NullReferenceException 1 Answer

Variables in components always returning null? 2 Answers

What is the basic syntax of accessing components? 1 Answer

NullReferenceException when setting Component as Variable, but no bugs 1 Answer

GetComponent, Object Reference not set to an instance of an object 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