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 $$anonymous$$ · May 04, 2013 at 08:11 AM · gameobjectnullreference

Null Reference Exception ? :/

 UnityEngine.GameObject.GetComponent<Player1> () (at C:/BuildAgent/work/7535de4ca26c26ac/Runtime/ExportGenerated/Editor/UnityEngineGameObject.cs:27)
 xa.Start () (at Assets/Scripts/xa.cs:30)

I get this error when i go to run my game

Here is my code

 using UnityEngine;
 using System.Collections;
 
     public class xa : MonoBehaviour 
     {
         public static Ball ball;
         public static AudioManager audioManager;
         public static ScoreManager scoreManager;
         
         public static Player1 player1;
         public static Player2 player2;
         //public static Player3 player3;
         //public static Player4 player4;
         
         public static bool gameOver = false;
         
         // set to false if game is configured for CTF
         public static bool letsPlayKeepaway = false; 
         
         // layers
         public const int Team1Goal = 9;
         public const int Team2Goal = 10;
         
         void Start()
         {
             // cache these so they can be accessed by other scripts
             ball = GameObject.FindWithTag("Ball").GetComponent<Ball>();
             scoreManager = gameObject.GetComponent<ScoreManager>();
             audioManager = gameObject.GetComponent<AudioManager>();
             player1 = GameObject.Find("Player1").GetComponent<Player1>();
             player2 = GameObject.Find("Player2").GetComponent<Player2>();
             //player3 = GameObject.Find("Player3").GetComponent<Player3>();
             //player4 = GameObject.Find("Player4").GetComponent<Player4>();
         }
     }

I dont know what i need to change or set ? If anyone has some advice?

Comment
Add comment · Show 7
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 $$anonymous$$ · May 04, 2013 at 09:11 AM 0
Share

I start with an empty scene and when i join the server it creates a player, but when it goes to start the match again the error comes up? any ideas?

avatar image Chronos-L · May 04, 2013 at 09:27 AM 0
Share

I have never work on a game/game-prototype that uses network/server, so I have no experience on that.

The only thing I am sure of is that Player1 does not exist (yet) when the GameObject.Find() is called in the Start() of the xa.cs.

avatar image $$anonymous$$ · May 04, 2013 at 10:29 AM 0
Share

how would i make it exist?

avatar image Chronos-L · May 04, 2013 at 10:38 AM 0
Share

By Instantiate()?

avatar image $$anonymous$$ · May 04, 2013 at 10:46 AM 0
Share

Do you have an example? Trying multiple ways but cant seem to write it correctly?

Show more comments

1 Reply

· Add your reply
  • Sort: 
avatar image
1

Answer by Chronos-L · May 04, 2013 at 09:07 AM

"Player1" doesn't exists in your scene, so GameObject.Find("Player1") returns a null and the null created the null-ref exception when you call GetComponent< Player1 > ().

Side Note: Please format your code properly for your own convenience. I did it for you this once.

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

13 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

Related Questions

Multiple Cars not working 1 Answer

A node in a childnode? 1 Answer

Player instantiates Backwords 0 Answers

Importing 2d graphics - adopting scene darkness - easy answer 0 Answers

Unity Networking, connecting to sever problems :( 1 Answer


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