• 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 vyriance · Jun 02, 2014 at 02:40 PM · null referance exeption

GameObject.Find/WithTag returns null

Ok guys so I'm having the following problem.

In the constructor of one of my scripts no matter what I do, the variable "player" is null, even though it shouldn't be.

 private StateManager manager;
     private GameObject player;
     private PlayerController controller;
     
     public OnRailsPlayState (StateManager managerRef) // Constructor
     {
         // Ensures that each State had a reference to the manager class // JS
         // manager of class Statemanger now holds a reference to StateManager // JS
         manager = managerRef;
         if(Application.loadedLevelName != "OnRailsPrototypeFinal")
             Application.LoadLevel("OnRailsPrototypeFinal");
         
         
         player = GameObject.Find ("SpeedCharacter");
         if(player != null)
             controller = player.GetComponent<PlayerController>();
         
         Debug.Log (player);
         Debug.Log (controller);

Yes there is a GameObject in the scene named "SpeedCharacter" just as it is typed there. The only thing I can think of is that it has something to do with the States system I'm trying to implement.

The Game starts in a scene that has a GameObject called GameManager in it with my stateManger script attached to it. At that point it is in the BeginSttae.

You click a buton in this scene and it switches you to a new scene and (state, the OnRailsPlayState listed here in code). In this scene is where the game object "SpeedCharacter" is.

So it shouldn't be null.

Any help at all would be greatly appreciated.

Thank you.

Comment
Add comment · Show 4
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 Jeff-Kesselman · Jun 02, 2014 at 02:41 PM 0
Share

I am a bit confused because this code fragment is not complete. Please show us the entire class as well as where this gets called from.

If this is a constructor, then this can't be a Unity component because they don't use constructors, so please show us the Unity component call that creates a new object of this type.

avatar image vyriance · Jun 02, 2014 at 02:52 PM 0
Share

Normally they do have start methods, but I'm setting this up via State$$anonymous$$achine. $$anonymous$$eaning, I have an interface that has the following in it.

 namespace Assets.Code.Interfaces 
 {
     public interface IStateBase
     {
         void StateUpdate();
         void ShowIt();
     
     }
 
 }

I then have a Stat$$anonymous$$anager that throws control of its Update and OnGui methods to the StateUpdate and ShowIt methods that are found in every single state once a given state is active.

the constructors of each State serve as the start methods.

The above state is being called from the ShowIt method of my BeginSttate via the following code

public void ShowIt() {

         const int buttonWidth = 84;
         const int buttonHeight = 60;
         
         // Draw a button to start the game
         if (GUI.Button(new Rect(Screen.width / 3,(2 * Screen.height / 3) - (buttonHeight / 2),buttonWidth,buttonHeight),"OnRails"))
         {
             // On Click, load the first level.
             // "Stage1" is the name of the first scene we created.
             Application.LoadLevel("OnRailsPrototypeFinal");
             Switch (1);
             //Debug.Log (manager.activeState);
         }
         
         // Draw a button to start the game
         // Center in X, 2/3 of the height in Y
         if (GUI.Button(new Rect(Screen.width / 2 + buttonWidth,(2 * Screen.height / 3) - (buttonHeight / 2),buttonWidth,buttonHeight),"Defense"))
         {
             // On Click, load the first level.
             // "Stage1" is the name of the first scene we created.
             Application.LoadLevel("DefensePrototype");
             Switch (2);
             
         }
     
     }


does that help clarify?

avatar image perchik · Jun 02, 2014 at 05:27 PM 0
Share

Do the Debug.Logs actually print something?

avatar image vyriance · Jun 03, 2014 at 07:53 AM 0
Share

The Debug.Logs print null

1 Reply

· Add your reply
  • Sort: 
avatar image
0

Answer by vyriance · Jun 05, 2014 at 12:32 AM

Hey guys I solved the problem. Basically I was switching states faster then the next scene was loading, thus the player object I declared in my script couldn't find the gameObject thus it was coming back as null.

Implemented a load screen to load between scene changes and now it works since it delays the swapping of states until the scene is loaded.

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.

Welcome to Unity Answers

If you’re new to Unity Answers, please check our User Guide to help you navigate through our website and refer to our FAQ for more information.

Before posting, make sure to check out our Knowledge Base for commonly asked Unity questions.

Check our Moderator Guidelines if you’re a new moderator and want to work together in an effort to improve Unity Answers and support our users.

Follow this Question

Answers Answers and Comments

23 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

Related Questions

Detecting gameObject from 3D rayCast 1 Answer

NullReference Exception when checking for Null 1 Answer

NullReferenceException: Object reference not set to an instance of an object 0 Answers

Random null reference exception error. 0 Answers

The object of type 'Transform' has been destroyed but I'm still trying to access it? 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