Results 1 to 4 of 4
  1. #1
    Join Date
    Aug 2004
    Location
    Italy
    Posts
    152

    Problems with Video 306 (Session 9, pt. 78)

    Hello,
    It's me again! I've encountered another problem while following along the video 306.

    After following along with the video and at the end of it, I try to run the code into Unity and I've got this very nice (not...) error:

    Note: I've changed the squared brackets in CODE directory in order not to upset the forum.
    Code:
    NullReferenceException: Object reference not set to an instance of an object
    PatchUpdater.Awake () (at Assets/(Code)/[Scripts]/PatchUpdater.cs:17)
    UnityEngine.GameObject:AddComponent()
    Code.TerrainSystem.<HandleTerrainPanning>c__AnonStorey2:<>m__2(IBundledAsset) (at Assets/(Code)/[TerrainSystem]/TerrainManager.cs:75)
    <LoadBundle>c__Iterator0:MoveNext() (at Assets/(Code)/[Scripts]/BundleManager.cs:100)
    It seems it can't load the asset with the m_Terrain textures. It points out the code inside the Awake method in the PatchUpdater.cs (patch.renderer.sharedMaterial line):

    PatchUpdater.cs
    Code:
        void Awake()
        {
            patch = transform.FindChild("terrainMesh").gameObject;
    
            patch.renderer.sharedMaterial = GameManager.CommonTerrainAssets.Load("m_Terrain") as Material;
    
            var tokens = gameObject.name.Substring(0, gameObject.name.IndexOf("(Clone)"))
                         .Replace("TerrainPatch_", "").Split('_');
    
            offset = new Vector2(int.Parse(tokens[0]), int.Parse(tokens[1]));
    
            patch.tag = "Terrain";
        }
    I should have exported all the assets correctly as instructed in the video number 304 but since the export procedure is not shown I may have selected the wrong export options in the terrain exporter editor, I'm not sure.

    I have to admit, though, that I did NOT get the "Asset Loaded" message in the unity editor console.

    I think the problem is somewhere here:

    GameManager.cs
    Code:
    void Start ()
        {
            // Attach the Budlemanager script to the gamemanager object
            gameObject.AddComponent<BundleManager>();
    
            new TerrainManager("Terrain");
    
            var url = string.Format("file:///{0}/{1}",
                                    Directory.GetCurrentDirectory(),
                                    "Data/Terrain/Common/Common.unity3d").Replace("\\", "/");
    
            BundleManager.RequestBundle(url, 
                (bundle) => { CommonTerrainAssets = bundle.AssetBundle; Debug.Log("Asset Loaded"); },
                "CommonTerrainAssets");    
            
        }
    As you cans ee, inside the RequestBundle call, it should at some point print "Asset Loaded" in the unity console, but it doesn't (Is this code executed? Maybe not).

    Another fuction I think it's relevant to this bug it's the HandleTerrainPanning() of the TerrainManager:

    TerrainManager.cs
    Code:
    private void HandleTerrainPanning()
    	    {
    	        var tempOffsets = new List<Vector2>();
    
    	        for (int z = -tileSideCount; z <= tileSideCount; z++)
                    for (int x = -tileSideCount; x <= tileSideCount; x++)
    	                tempOffsets.Add(new Vector2(CameraUtilities.GetCurrentPatch().x + x,
                                                    CameraUtilities.GetCurrentPatch().y + z));
    
    	        var offsets = tempOffsets.OrderBy(patch => Vector2.Distance(patch, CameraUtilities.GetCurrentPatch()));
    
    	        foreach (var offset in offsets)
    	        {
    	            var camRotY = Camera.main.transform.rotation.eulerAngles.y * Mathf.Deg2Rad;
                    var camFwd = new Vector2(Mathf.Sin(camRotY), Mathf.Cos(camRotY));
    
                    if (!Patches.ContainsKey(offset) &&
                        (Vector2.Dot(camFwd, (offset - CameraUtilities.GetCurrentPatch()).normalized) > 0.5f ||
                         Vector2.Distance(offset, CameraUtilities.GetCurrentPatch()) <= 0.8f))
                    {
                        var patchName = string.Format("TerrainPatch_{0}_{1}", offset.x, offset.y);
                        var zone = CameraUtilities.GetCurrentZone(offset);
                        var zoneName = string.Format("Zone_{0}_{1}", zone.x, zone.y);
                        var url = string.Format("file:///{0}/{1}/{2}/{3}.unity3d",
                                                Directory.GetCurrentDirectory(),
                                                "Data/Terrain/Patches",
                                                zoneName,
                                                patchName).Replace("\\", "/");
    
                        GameObject patch = null;
                        Patches.Add(offset, patch);
    
                        var position = new Vector3(offset.x, 0, offset.y);
    
                        BundleManager.RequestBundle(url, (bundle) =>
                        {
                            patch = Object.Instantiate(bundle.AssetBundle.mainAsset) as GameObject;
                            if (patch != null)
                            {
                                patch.AddComponent<PatchUpdater>();
                                patch.transform.position = position*CameraUtilities.PatchSize;
                                patch.transform.parent = TerrainObject.transform;
                            }
                        });
                    }
    	        }
    	    }
    The very last function that may be related to this issue is the LoadBundle method from the BundleManager:

    BundleManager.cs
    Code:
    static IEnumerator LoadBundle(IBundledAsset bundle)
        {
            // Coroutine that will load a bundle
            var www = new WWW(bundle.Url);
    
            // Stop here untill www is done loading!
            yield return www;
    
            // ok!
            if (string.IsNullOrEmpty(www.error))
            {
                // If www is empty or null, we know we got something back from our request
    
                try
                {
                    bundle.AssetBundle = www.assetBundle;
                    bundle.IsDone = true;
    
                    if (string.IsNullOrEmpty(bundle.Name))
                    {
                        // if this is NULL we're going to create a bundlename based of the url
                        bundle.Name = bundle.Url.Substring(0, bundle.Url.LastIndexOf(".unity3d"))
                                      .Substring(bundle.Url.LastIndexOf("/") + 1);
    
                        Bundles.Add(bundle.Name, bundle);
    
                        bundle.Callback(bundle);
                    }
                }
                catch (Exception ex)
                {
                    Debug.LogWarning("Something went wrong with the setting up the asset bundle.");
                    Debug.Log(ex.Message);
                }
            }
            else
            {
                // Something went wrong!
                Debug.LogError(www.error);
                currentBundle = null;
            }
        }
    I have the impression that IEnumerator may be the culprit.

    Can you help me figuring the problem out? Thanky ouv ery much and please tell me if you require some other portion of code
    Last edited by Artanis; 11-03-2011 at 11:54 AM. Reason: More info added
    Artanis

    My Blog (Italian): http://www.artanis.it
    My Web Site (Italian/English): http://www.fabriziotobia.com
    My Community (Italian): http://www.darksithlords.it

  2. #2
    Join Date
    Aug 2004
    Location
    Italy
    Posts
    152
    Oh may I ask you to tell me precisely how I should export all the data in the Terrain Exporter? Maybe the problem is there, I may have exported them in the wrong way

    Thank you again ^_^
    Artanis

    My Blog (Italian): http://www.artanis.it
    My Web Site (Italian/English): http://www.fabriziotobia.com
    My Community (Italian): http://www.darksithlords.it

  3. #3
    Join Date
    Aug 2004
    Location
    Italy
    Posts
    152
    Any tip? I was thinking that maybe you could upload somewhere the .unity3d package with the terrain. Maybe it's an exporter problem and not a viewer problem?
    Artanis

    My Blog (Italian): http://www.artanis.it
    My Web Site (Italian/English): http://www.fabriziotobia.com
    My Community (Italian): http://www.darksithlords.it

  4. #4
    Join Date
    Aug 2004
    Location
    Italy
    Posts
    152
    I guess this time the problem is harder to debug than I thought! I may have to put the MMO Class aside for a bit if I can't figure it out soon. If anyone has some tips, please feel free to let me know

    Thanks!
    Artanis

    My Blog (Italian): http://www.artanis.it
    My Web Site (Italian/English): http://www.fabriziotobia.com
    My Community (Italian): http://www.darksithlords.it

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •