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
1
Question by CG-DJ · Apr 23, 2014 at 09:03 PM · editornullreferenceexceptioncloneinvisible

Unity Creating Invisible Clone (C#)

Hey Guys

I have a enemy script that looks at a variable from the player script. So I have local variables in the enemy script for the player, and various scripts attached to the player. The problem is, sometimes Unity will create an invisible clone of the player object that doesn't show up in the hierarchy, but it is what Unity finds first when looking for a player object. So all of my script references get broken and throws up NullReferenceExceptions because this clone has no scripts attached (i guess). I have noticed that this always happens whenever I mess with Mecanim settings.

Here is the code:

 void Start () 
     {
         player = GameObject.FindGameObjectWithTag("Player");  //player is the Player in the scene.
         playerScript = player.GetComponent<TopLevelPlayerScript>();  //playerScript is the TopLevel script
         combatScript = player.GetComponent<BaseCombatSystem>(); //combatScript is the BaseCombatScript.
         healthScript = GetComponent<Health>();
         anim = GetComponent<Animator>(); //This is the animator component for the Orc.
 
         thisRenderer.material = diffuseMat;
 
         anim.applyRootMotion = false;  //This is here just for testing purposes.  I didn't want some of the animations to apply root animation.
     }
 
     void Update () 
     {
         print (player.name); //This is for debgging purposes
 
         if(combatScript.target == this.gameObject)  //If the player is highlighing us
         {
             thisRenderer.material = outlinedMat;  //Change the material to the outline material
         }
         else  //If we aren't being looked at
         {
             if(thisRenderer.material != diffuseMat) //Switch to diffuse.
             {
                 thisRenderer.material = diffuseMat;
             }
         }

I've read around online, and discovered the fix is to restart Unity. Restarting unity fixes the problem, but any time I change anything in Mecanim the error pops up again, forcing me to restart unity. Its kinda annoying.

So do you think this is a me problem, or a bug in Unity? Should I write a bug report? Do you need any information to help?

Thanks in advance

Comment
Add comment · Show 1
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 CG-DJ · Apr 29, 2014 at 03:41 AM 0
Share

Any Ideas?

6 Replies

· Add your reply
  • Sort: 
avatar image
0

Answer by Dameon_ · Apr 29, 2014 at 04:06 PM

I'm not entirely sure if it's a bug, but your implementation seems fine.

If Unity really is making a duplicate object, the workaround is to get all the objects with the player tag, and search them for the valid player object.

     GameObject[] playerList = GameObject.FindGameObjectsWithTag("Player"); // Get a list of all the objets with the player tag into an array
     for (int i = 0; i < playerList.Length(); i++)
     {
          if (playerList[i].GetComponent<TopLevelPlayerScript>() != null) // See if we can find the script on the returned object
          {
               player = PlayerList[i];
               break;
          }
     }
     if (player == null) { // Check to make sure we found the player.
          Debug.LogError("Unable to find player object!");
          return; // Prevent any NullExceptionErrors
     }
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
avatar image
0

Answer by kvick · May 26, 2014 at 12:05 AM

I've been having the same error too, a temporary fix for me is to just restart Unity, but it doesn't really fix the issue overall. I tried reinstalling, haven't had the problem again yet, but would like to find a solution as well.

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
avatar image
0

Answer by raiden · Dec 26, 2014 at 09:47 PM

I have also had this issue, restarting Unity now. Possibly a bug, hopefully will be resolved in the 4.x versions.

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
avatar image
0

Answer by barbe63 · Jan 21, 2015 at 09:19 AM

Attack of the clones!!

Same issue, same solution, restart unity... I guess when you have to restart the editor to makes your game run fine, it is safe to call it a bug. The only editor scripts i have now, i didn't had them when this issue started to show up.

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
avatar image
0

Answer by ignacio-casal · Jan 20, 2016 at 04:09 AM

The same have happened to me a couple of times lately. Also fixed by restarting the editor.

On thing I think I noticed is that it happens if I change another object to the Player layer, make it prefab, remove the layer to the prefab. Seems like cache problem to me, not sure.

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
  • 1
  • 2
  • ›

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

26 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

Related Questions

Is it possible to get a cloned gameObject's parent name? 1 Answer

Manually creating Editor sometimes leads to NullReferenceException in SerializedObject constructor 0 Answers

How to make changes to a copied object 1 Answer

NullReferenceException on GameObjectInspector Editor Error 6 Answers

Object not selected in hierarchy gives an error 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