Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice

Question Component at index 2 could not be loaded when loading game object 'MainCamera'. Removing it!

Discussion in 'Editor & General Support' started by thothgreen, Jun 26, 2020.

  1. thothgreen

    thothgreen

    Joined:
    Jun 25, 2020
    Posts:
    3
    How do I solve this error?
     
    shaneparsons likes this.
  2. shaneparsons

    shaneparsons

    Joined:
    May 5, 2015
    Posts:
    44
    Did you ever figure this out? I've been having the same issue since I updated my project to the latest version of Unity.
     
    kgluschenko83 and jereenvalsson like this.
  3. shaneparsons

    shaneparsons

    Joined:
    May 5, 2015
    Posts:
    44
    Disregard my question, I just resolved it... In my case I just had to remove the "Flare Layer" component that was attached to the main camera of all of my scenes.
     
  4. mrdanielwu

    mrdanielwu

    Joined:
    Jul 24, 2020
    Posts:
    1
    This happened when I was importing an asset...
     
  5. salchicha_loca

    salchicha_loca

    Joined:
    Feb 20, 2018
    Posts:
    3
    Maybe too late but I solve this same problem installint unity 2018 lts
     
  6. JackRobertDuncan

    JackRobertDuncan

    Joined:
    Sep 23, 2020
    Posts:
    1
    I deleted flare layer it isnt working tho
     
  7. Cagoo0

    Cagoo0

    Joined:
    Sep 24, 2020
    Posts:
    1
    i have same problem
     
  8. monta

    monta

    Joined:
    Sep 1, 2014
    Posts:
    1
    Had the same issue.
    I think I found the answer here:
    https://issuetracker.unity3d.com/is...ject-exception-is-thrown-on-opening-the-scene

    Summarised solution:

    Just before this warning in the console there is another warning:
    Component GUI Layer in Main Camera for Scene Assets/SortingLayerExample.unity is no longer available.
    It will be removed after you edit this GameObject and save the Scene.

    Basically the Component type no longer exists and it will be removed next time the scene is saved.
     
  9. ramzeen666

    ramzeen666

    Joined:
    Oct 13, 2020
    Posts:
    1
    I'm having the same issue with Unity 2019, anyone know how to solve this?
     
  10. NecXoo

    NecXoo

    Joined:
    Oct 25, 2020
    Posts:
    1
    OMG This editor is so f... bugged even after 10 years not using it
     
    oBUF likes this.
  11. hasanmusa05g

    hasanmusa05g

    Joined:
    Nov 27, 2020
    Posts:
    1
    Just delete folder which one is causing to error
     
  12. damdam17

    damdam17

    Joined:
    Dec 2, 2020
    Posts:
    2
    can any body tell me in easy way to solve the error
     
  13. dubhorizoned

    dubhorizoned

    Joined:
    Dec 2, 2020
    Posts:
    1
    Just click main camera and check the components in the inspector. For me there was a missing script.
     
  14. PE-h89

    PE-h89

    Joined:
    Jan 5, 2021
    Posts:
    1
    im having this problem with some free assets i got from the store i dont know hoow to solve this issue
     
    Nicobr2207 likes this.
  15. erdem2706

    erdem2706

    Joined:
    Nov 27, 2020
    Posts:
    1
    Sometimes assets include a demo scene. Open this demo scene and as shaneparsons already pointed out, simply delete the "Flare Layer" which is assigned to the "Main Camera".
     
    Nastomeya and MiguelAppStudio like this.
  16. EshaNTR

    EshaNTR

    Joined:
    Jan 24, 2021
    Posts:
    1
    No way to solve that F***ing problem.
     
    Soshiyanet likes this.
  17. Soshiyanet

    Soshiyanet

    Joined:
    Feb 6, 2017
    Posts:
    3
    Feb 18, 2021
    Not a solution Yet
     
  18. markmamaniy

    markmamaniy

    Joined:
    Feb 22, 2021
    Posts:
    1
    how do you delete the flare layer I'm new
     
    mercuryVapor likes this.
  19. DanteRexe

    DanteRexe

    Joined:
    Aug 28, 2020
    Posts:
    1
    I actually just uncheck the flare Layer of the camera and works perfectly, but I don't know what the problem was
     
  20. jhesmael

    jhesmael

    Joined:
    May 3, 2021
    Posts:
    1
    I had this problem with Standard Assets, but I noticed that it said it was for 1.8 version of unity, so it will not even work in the latest version!
     
    Team_SeMi likes this.
  21. Zekbot01

    Zekbot01

    Joined:
    Mar 31, 2021
    Posts:
    1
    I had the same problem. I solved it, with deleting a script in Assest\Standart Assets\Utilitiy\. The Script's name is in the Console. If you go back in the console, you should see an error at somewhere at the location above. You have to delete the script, where the error is, and then you should have no problems.
     
    honeygryndeer and Team_SeMi like this.
  22. IWantToMakeGamesHowPro

    IWantToMakeGamesHowPro

    Joined:
    Jun 24, 2021
    Posts:
    1
    i have the same problem on unity 2020.3 pls help
     
  23. Bob6767

    Bob6767

    Joined:
    May 28, 2021
    Posts:
    2
    I've deleted the camera and it comes up with the same error.

    (Dont worry I found a solution)










    Give up
     
    Last edited: Jul 8, 2021
  24. pampersss

    pampersss

    Joined:
    Jul 17, 2021
    Posts:
    1
    What is solution pls help
     
  25. jkgamer2k20

    jkgamer2k20

    Joined:
    Jul 18, 2021
    Posts:
    1
    I went to the code part and changed "public GUIText <coderdefinedname>" to "public Text <coderdefinedname>" after including "using unityEngine.UI" in header.
    the code is at asset >standard assets>utility>simpleactivatormenu.cs

    this helped in my case
     
    darkdoom973 and oscar-laguna like this.
  26. PureXHero

    PureXHero

    Joined:
    Aug 27, 2021
    Posts:
    2
    Find the asset in the "Asset store", there you can see which version of unity is needed.
     
  27. unity_900470C8B053FBD3B53A

    unity_900470C8B053FBD3B53A

    Joined:
    Sep 12, 2021
    Posts:
    2
    fix has been found and provided by
    U
    unity_1E53943FC6A3BD3E0A28on version 1.1.6



    Error fix is easy! Here's how!
    18 days ago
    - Open Assets/StandardAssets/Utility/SimpleActivatorMenu.cs - change "GUIText" to "UnityEngine.UI.Text". (GUIText is officially removed in newer versions of Unity, but the package isn't maintained) - Press play on your scene to get rid of the remaining compile error warnings. - Enjoy! Serves as a great starting point for lots of different types of projects. Really digging it!

    it worked great for me.
     
    Last edited: Sep 13, 2021
  28. Rainbow1444

    Rainbow1444

    Joined:
    Sep 14, 2021
    Posts:
    2
    Thanks! This worked. So easy.:p
     
  29. abidmahmudh

    abidmahmudh

    Joined:
    Sep 30, 2021
    Posts:
    1
  30. darkdoom973

    darkdoom973

    Joined:
    Jul 21, 2020
    Posts:
    13
    This one works!!!!!
     
  31. inabird

    inabird

    Joined:
    Jun 12, 2021
    Posts:
    1
    it works! Thank you so much
     
  32. skyemont

    skyemont

    Joined:
    Dec 1, 2020
    Posts:
    3
    This error (or its sister side effect of the component not being added, even though your editor code said it was) can occur for various reasons. I don't have standard assets in my project at all and the solution above about GUIText was of no use to me. I had an editor script that generated components on objects based upon what folder they were in. I was getting this error a lot, and I finally narrowed it down to the fact that unity didn't seem to be able to handle adding components to thousands of asset game objects in a single pass in the editor script.

    Once I reduced it to work in passes of 300-1000 it seemed to do just fine. I had a max counter and if I hit that I stopped and logged a message that max was reached and to run it again. I didn't try above 1000 as it was enough for my needs to just hit it until it was done. If you have millions you may want to try a few more at a time. :)

    Also, I was using the AddComponent(Type) method, but also tested adding a component with the direct generic AddComponent<MyClass>() and still had the issue.
     
  33. hanxiaohhh

    hanxiaohhh

    Joined:
    Jul 19, 2022
    Posts:
    2
    me too.....
     
  34. JakeLovesCoding

    JakeLovesCoding

    Joined:
    Mar 24, 2023
    Posts:
    1
    same
     
  35. the_Whizzkid

    the_Whizzkid

    Joined:
    Apr 15, 2020
    Posts:
    20
    A bit off topic, but I landed here.
    I had this error (no Maincamera, but colliders) on 2 Prefabs with Capsule Collider on a child object, while spawning them.
    At the end I removed both capsule collider and added them again. Worked. (Probably corrupted somehow).
     
  36. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    36,954
    I wrote a script to remove all the GUILayers and the FlareLayers from all cameras.

    BE CAREFUL WHAT YOU RUN THIS ON! BACK UP EVERYTHING FIRST!!!

    Code (csharp):
    1. // You must run this on an old enough version of Unity that
    2. // still has GUILayers and FlareLayer objects, obviously.
    3.  
    4. #if !UNITY_2019_1_OR_NEWER
    5.  
    6. using UnityEngine;
    7. using System.Collections;
    8. using UnityEditor.Callbacks;
    9. using UnityEditor;
    10. using UnityEditor.SceneManagement;
    11.  
    12. // 12:31 PM 10/22/2022 - GUILayers handled
    13. // 7:27 AM 5/27/2023 - FlareLayers handled too
    14.  
    15. public class KurtUpgradeTools : MonoBehaviour
    16. {
    17.     [MenuItem( itemName: "Assets/Remove GUI Flare Layers")]
    18.     public static void RemoveAllGUILayers()
    19.     {
    20.         // - find all scenes
    21.         string[] sceneGUIDs = AssetDatabase.FindAssets( "t:scene");
    22.         int numScenes = sceneGUIDs.Length;
    23.  
    24.         Debug.Log( System.String.Format( "We found {0} scenes in total.", numScenes));
    25.  
    26.         int scenesConsidered = 0;
    27.         int scenesProcessed = 0;
    28.  
    29.         int GUILayersRemoved = 0;
    30.         int FlareLayersRemoved = 0;
    31.  
    32.         int DidNotRemoves = 0;
    33.  
    34.         // - iterate and open each scene
    35.         for (int i = 0; i < numScenes; i++)
    36.         {
    37.             scenesConsidered++;
    38.  
    39.             var sceneName = AssetDatabase.GUIDToAssetPath( sceneGUIDs[i]);
    40.  
    41.             // shortenings
    42. //            if (i > 20) continue;
    43. //
    44. //            if (!sceneName.Contains( "zeroscene"))
    45. //            {
    46. //                continue;
    47. //            }
    48.  
    49.             Debug.Log( System.String.Format( "Working on sceneName '{0}'...", sceneName));
    50.  
    51.             var scene = EditorSceneManager.OpenScene( sceneName);
    52.  
    53.             bool needSave = false;
    54.  
    55.             // Do the GUILayers...
    56.             {
    57.                 // - find and Destroy() any and all GUILayer instances
    58.                 GUILayer[] GUILayers = Resources.FindObjectsOfTypeAll<GUILayer>();
    59.  
    60.                 // Only available in later versions of Unity. Fail.
    61.                 // GUILayer[] GUILayers = FindObjectsOfType<GUILayer>( includeInactive: true);
    62.  
    63.                 if (GUILayers != null && GUILayers.Length > 0)
    64.                 {
    65.                     Debug.Log( System.String.Format( "... which has '{0}' GUILayers.", GUILayers.Length));
    66.  
    67.                     foreach( var gl in GUILayers)
    68.                     {
    69.                         var go = gl.gameObject;
    70.  
    71.                         Debug.Log( System.String.Format( "Contemplating the GUILayer on {0}...", go.name));
    72.  
    73.                         // from docs at: https://docs.unity3d.com/ScriptReference/Resources.FindObjectsOfTypeAll.html
    74.                         // This will filter for GameObjects in the scene:
    75.                         //
    76.                         // APPARENTLY this is incorrect and causes it to FAIL on older scenes. Fail
    77.                         //
    78.                         if (!EditorUtility.IsPersistent(go.transform.root.gameObject)
    79.                             &&
    80.                             !(
    81.                                 (go.hideFlags == HideFlags.NotEditable)
    82.                                 ||
    83.                                 (go.hideFlags == HideFlags.HideAndDontSave)
    84.                             )
    85.                         )
    86.                         {
    87.                             GUILayersRemoved++;
    88.  
    89.                             Debug.Log( System.String.Format( "Removed GUILayer from {0}/{1}...", sceneName, go.name));
    90.  
    91.                             DestroyImmediate( gl);
    92.                         }
    93.                         else
    94.                         {
    95.                             DidNotRemoves++;
    96.                             Debug.LogError( System.String.Format( "Failed to remove GUILayer on {0}/{1}!!!!!!!!!!!", sceneName, go.name));
    97.                         }
    98.                     }
    99.  
    100.                     needSave = true;
    101.                 }
    102.                 else
    103.                 {
    104.                     Debug.LogWarning( "... has no GUILayers.");
    105.                 }
    106.             }
    107.  
    108.             // Do the FlareLayers...
    109.             {
    110.                 // - find and Destroy() any and all FlareLayer instances
    111.                 FlareLayer[] FlareLayers = Resources.FindObjectsOfTypeAll<FlareLayer>();
    112.  
    113.                 // Only available in later versions of Unity. Fail.
    114.                 // FlareLayer[] FlareLayers = FindObjectsOfType<FlareLayer>( includeInactive: true);
    115.  
    116.                 if (FlareLayers != null && FlareLayers.Length > 0)
    117.                 {
    118.                     Debug.Log( System.String.Format( "... which has '{0}' FlareLayers.", FlareLayers.Length));
    119.  
    120.                     foreach( var fl in FlareLayers)
    121.                     {
    122.                         var go = fl.gameObject;
    123.  
    124.                         // Apparently this also finds the FlareLayer on the SceneCamera!
    125.                         // All the .hideFlags and .activeSelf and .activeInHierarchy are all identical so
    126.                         // I am going to have to recognize the damned thing by name. Fail.
    127.                         if (go.name.Equals( "SceneCamera"))
    128.                         {
    129.                             Debug.Log( "Ignoring SceneCamera...");
    130.                             continue;
    131.                         }
    132.  
    133.                         Debug.Log( System.String.Format( "Contemplating the FlareLayer on {0}...", go.name));
    134.  
    135.                         // from docs at: https://docs.unity3d.com/ScriptReference/Resources.FindObjectsOfTypeAll.html
    136.                         // This will filter for GameObjects in the scene:
    137.                         //
    138.                         // APPARENTLY this is incorrect and causes it to FAIL on older scenes. Fail
    139.                         //
    140.                         if (!EditorUtility.IsPersistent(go.transform.root.gameObject)
    141.                             &&
    142.                             !(
    143.                                 (go.hideFlags == HideFlags.NotEditable)
    144.                                 ||
    145.                                 (go.hideFlags == HideFlags.HideAndDontSave)
    146.                             )
    147.                         )
    148.                         {
    149.                             FlareLayersRemoved++;
    150.  
    151.                             Debug.Log( System.String.Format( "Removed FlareLayer from {0}/{1}...", sceneName, go.name));
    152.  
    153.                             DestroyImmediate( fl);
    154.                         }
    155.                         else
    156.                         {
    157.                             DidNotRemoves++;
    158.                             Debug.LogError( System.String.Format( "Failed to remove FlareLayer on {0}/{1}!!!!!!!!!!!", sceneName, go.name));
    159.                         }
    160.                     }
    161.  
    162.                     needSave = true;
    163.                 }
    164.                 else
    165.                 {
    166.                     Debug.LogWarning( "... has no FlareLayers.");
    167.                 }
    168.             }
    169.  
    170.             if (needSave)
    171.             {
    172.                 scenesProcessed++;
    173.  
    174.                 EditorSceneManager.MarkSceneDirty( scene);
    175.  
    176.                 // - resave the scene
    177.                 EditorSceneManager.SaveScene(scene);
    178.  
    179.                 EditorSceneManager.NewScene( NewSceneSetup.EmptyScene);
    180.             }
    181.  
    182.         }
    183.  
    184.         {
    185.             string s = "FINAL REPORT ON UPGRADING TO REMOVE GUILayers/FlareLayers:\n";
    186.             s += System.String.Format( "Considered {0} scenes.\n", scenesConsidered);
    187.             s += System.String.Format( "Processed {0} scenes.\n", scenesProcessed);
    188.             s += System.String.Format( "Removed {0} GUILayers.\n", GUILayersRemoved);
    189.             s += System.String.Format( "Removed {0} FlareLayers.\n", FlareLayersRemoved);
    190.             if (DidNotRemoves > 0)
    191.             {
    192.                 s += "<b><color=#ff8080>";        // red
    193.             }
    194.             else
    195.             {
    196.                 s += "<b><color=#80ff80>";        // green
    197.             }
    198.             s += System.String.Format( "Unable to remove {0} things. See above.</color></b>\n", DidNotRemoves);
    199.             Debug.Log( s);
    200.         }
    201.     }
    202. }
    203. #endif

    Here's a more generic "do something to all scenes" one that doesn't do anything yet:

    https://gist.github.com/kurtdekker/232f56112a204d574402bae9a7fe3ba3

    See comments in code for where you do the thing(s) you want to do to the scene(s).
     
    anuragaofl likes this.