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 Doctor_Gonzo · Mar 18, 2014 at 06:40 PM · instantiatephotonnullreferenceexception

Null Reference when trying to instantiate a prefab not in the scene

So I'm driving myself insane trying to nail down this particular bug. Basically, I have a multiplayer only game, and when you first start it up, you are presented with a toolbar of buttons to select the color for your tank. Upon pressing the button, it SHOULD instantiate the tank, the child GameObject turret, and it's associated scripts. The problem is, I can't seem to pass the public variable along to the proper scripts. I know this probably doesn't make much sense, so here's some code. First the NetworkManager script :

  public class NetworkManager : MonoBehaviour {
     
         public GameObject selectedTank;
         private int selectedToolbar = 0;
         private string[] toolbarStrings = { "Gray", "Green", "Purple", "Red", "Yellow" };
         public bool canSelectColor = true;
     
         void Start () 
         {
             PhotonNetwork.ConnectUsingSettings("alpha");
         }
     
         void OnGUI()
         {
             
             if (canSelectColor)
             {
                 selectedToolbar = GUI.Toolbar(new Rect(50, 10, Screen.width - 100, 30), selectedToolbar, toolbarStrings);
                 if (GUI.changed)
                 {
                     if (0 == selectedToolbar)
                     {
                         selectedTank = (GameObject)Resources.Load("grayTank");
                         canSelectColor = false;
                         PhotonNetwork.JoinRandomRoom();
                     }
                     if (1 == selectedToolbar)
                     {
                         selectedTank = (GameObject)Resources.Load("greenTank");
                         canSelectColor = false;
                     }
                     if (2 == selectedToolbar)
                     {
                         selectedTank = (GameObject)Resources.Load("purpleTank");
                         canSelectColor = false;
                     }
                     if (3 == selectedToolbar)
                     {
                         selectedTank = (GameObject)Resources.Load("redTank");
                         canSelectColor = false;
                     }
                     if (4 == selectedToolbar)
                     {
                         selectedTank = (GameObject)Resources.Load("yellowTank");
                         canSelectColor = false;
                     }
                 }
                 GUILayout.Label(PhotonNetwork.connectionStateDetailed.ToString());
             }
         }
         //void OnJoinedLobby()
         //{
         //    PhotonNetwork.JoinRandomRoom();
             
         //}
     
         void OnPhotonRandomJoinFailed()
         {
             PhotonNetwork.CreateRoom(null);
         }
     
         void OnJoinedRoom()
         {
             GameObject tank = PhotonNetwork.Instantiate(selectedTank.name, Vector3.zero, Quaternion.identity, 0);
             FireControl fireControl = tank.GetComponentInChildren<FireControl>();
             fireControl.enabled = true;
             TurretControl turretControl = tank.GetComponentInChildren<TurretControl>();
             turretControl.enabled = true;
             TankControl tankControl = tank.GetComponent<TankControl>();
             tankControl.enabled = true;
         }
     
        
     }

And the TurretControl script, where all the problems seem to be happening :

 public class TurretControl : MonoBehaviour {
     private GameObject tank;
     public GameObject selectedTank;
     
    
 
     // Use this for initialization
     void Start () 
     {
         //tank = GameObject.Find("redTank(Clone)");
         selectedTank = GetComponent<NetworkManager>().selectedTank;
         
         
     }
 
     void Awake()
     {
       
       
     }
     // Update is called once per frame
     void Update () 
     {
         Debug.Log(selectedTank.name);
         Vector3 turretPos = new Vector3(selectedTank.transform.position.x, selectedTank.transform.position.y, -3);
         Vector3 rotation = Input.mousePosition - Camera.main.WorldToScreenPoint(transform.position);
         float angle = Mathf.Atan2(rotation.x, rotation.y * -1) * Mathf.Rad2Deg;
         transform.rotation = Quaternion.Euler(new Vector3(0, 0, angle - 90));
         transform.position = turretPos;
     }
 
 }

What is happening is it spawns in the grayTank just fine, but the turret is drawn in the wrong location, not locked to the tank properly. Also Unity is throwing NullReference exceptions left and right, saying that selectedTank is null. I don't understand how that can be when it is clearly spawning the tank onto the screen. Any help is greatly appreciated.

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

0 Replies

· Add your reply
  • Sort: 

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

20 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

Related Questions

Referencing gameObject from script after Instantiate 0 Answers

Photon.Instantiate causes second-created player to swap places with first 1 Answer

Photon Instantiate object not disappear after dc master client 0 Answers

NullReference Exception when adding a class 2 Answers

[Closed]NullReferenceException on Object Instantiation onto game world 2 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