Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 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 LedomDeveloping · Jul 15, 2017 at 05:35 PM · terrains

"ArgumentException: The Object you want to instaintiate is null."

Hi everyone,

Recently I'm getting this ArgumentException while attempting to instantiate a Terrain.
I first click on a button, then the Run() void (code below) is executed.
The exception is thrown at line 33.
Code for LevelLoader.cs:

 using System.Collections;
 using System.Collections.Generic;
 using UnityEngine;
 using UnityEngine.SceneManagement;
 
 public class LevelLoader : MonoBehaviour {
     public class LevelElemTag
     {
         public Dictionary<string, string> TagProperties = new Dictionary<string, string>();
     }
 
     public string fileName = "";
     public void Run() {
         if (!System.IO.File.Exists(fileName)) { throw new System.Exception("File " + fileName + " not found!"); };
         //Load file content:
         string[] fileContent = System.IO.File.ReadAllLines(fileName, System.Text.Encoding.Unicode);
         //Process file:
         foreach(string s in fileContent) //For each element (represented as a text line in the file)
         {
             //Read base properties:
             Vector3 position = GetVector3(s,1);
             Quaternion rotation = GetQuaternion(s, 2);
             LevelElemTag data = GetTag(s, 3);
             //What is -s-?
             switch (GetNature(s))
             {
                 case "tran":
                     //It's a train model
                     Instantiate(GetTrain(data), position, rotation);
                     break;
                 case "terr":
                     //It's a terrain
                     Instantiate(GetTerrain(data), position, rotation);
                     break;
                 case "rail":
                     //It's a rail
                     Instantiate(GetRailway(data), position, rotation);
                     break;
                 case "deco":
                     //It's a decoration
                     Instantiate(GetDecor(data), position, rotation);
                     break;
                 case "virt":
                     //It's an entity (controller, effect...)
                     Instantiate(GetEntity(data), position, rotation);
                     break;
                 default:
                     break;
             }
         }
 
     }
     string GetNature(string line)
     {
         return line.Split('|')[0];
     }
     Vector3 GetVector3(string line,int index)
     {
         string block = line.Split('|')[index];
         float x = 0, y = 0, z = 0;
         try
         {
         x = float.Parse(block.Split(';')[0]);
         y = float.Parse(block.Split(';')[1]);
         z = float.Parse(block.Split(';')[2]);
         }
         catch (System.Exception e)
         {
             throw e;
         }
         return new Vector3(x, y, z);
     }
     Quaternion GetQuaternion(string line, int index)
     {
         string block = line.Split('|')[index];
         float w = 0,x = 0, y = 0, z = 0;
         try
         {
             w = float.Parse(block.Split(';')[0]);
             x = float.Parse(block.Split(';')[1]);
             y = float.Parse(block.Split(';')[2]);
             z = float.Parse(block.Split(';')[3]);
         }
         catch (System.Exception e)
         {
             throw e;
         }
         return new Quaternion(x,y,z,w);
     }
     LevelElemTag GetTag(string line, int index)
     {
         string block = line.Split('|')[index];
         LevelElemTag my = new LevelElemTag();
         foreach (string item in block.Split(';'))
         {
             my.TagProperties.Add(item.Split('=')[0], item.Split('=')[1]);
         }
         return my;
     }
 
     GameObject GetTrain(LevelElemTag meta)
     {
         GameObject model = (GameObject) Resources.Load("Trains/" + meta.TagProperties["family"] + "/" + meta.TagProperties["type"]) as GameObject;
         return model;
     }
     Terrain GetTerrain(LevelElemTag meta)
     {
         Terrain model = (Terrain)Resources.Load("Objects/Terrains/" + meta.TagProperties["family"] + "/" + meta.TagProperties["type"]) as Terrain;
         return model;
     }
     GameObject GetRailway(LevelElemTag meta)
     {
         GameObject model = (GameObject)Resources.Load("Objects/Rails/" + meta.TagProperties["family"] + "/" + meta.TagProperties["type"]) as GameObject;
         return model;
     }
     GameObject GetDecor(LevelElemTag meta)
     {
         GameObject model = (GameObject)Resources.Load("Objects/Decor/" + meta.TagProperties["family"] + "/" + meta.TagProperties["type"]) as GameObject;
         return model;
     }
     GameObject GetEntity(LevelElemTag meta)
     {
         GameObject model = (GameObject)Resources.Load("Objects/Entities/" + meta.TagProperties["family"] + "/" + meta.TagProperties["type"]) as GameObject;
         return model;
     }
 }
 

Directory structure for the terrain:
Assets/Objects/Terrains/debug/grassy.prefab (as in the editor's Project window)

Values at runtime:
family is debug
type is grassy

Thanks in advance
Lorenzo

Comment
Add comment
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 Reply

· Add your reply
  • Sort: 
avatar image
1
Best Answer

Answer by Bunny83 · Jul 15, 2017 at 06:31 PM

Your "Objects" folder is not located in a Resources folder. You can use Resources.Load only for assets stored in a Resources folder. Any other asset is not included in your build when it's not directly referenced by a script / component.

So the path should be

 Assets/Resources/Objects/Terrains/debug/grassy.prefab 

Btw: You currently do a double casting of the result of Resources.Load. Just use the generic version and remove all the casting:

 Terrain model = Resources.Load<Terrain>("Objects/Terrains/" + meta.TagProperties["family"] + "/" + meta.TagProperties["type"]);

I also want to note that the way you parse your string is extremely inefficient. string.split will allocate a new array each time it is called.

Comment
Add comment · Show 1 · 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 LedomDeveloping · Jul 16, 2017 at 09:44 AM 0
Share

Thank you for your answer @Bunny83 , but, how could I split the string in a more efficient way? Lorenzo

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

69 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 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 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

Access terrains "Alphamap 0" from within code 0 Answers

The problem of making large terrains 0 Answers

Terrain matrix 8*8 1 Answer

A question about terrain collision 1 Answer

How to export heightmap of a terrain during runtime. 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