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 /
  • Help Room /
avatar image
0
Question by neosca · Feb 08, 2017 at 11:48 AM · gameobjectsprefab-instanceresources.loadallresource.load

The Object you want to instantiate is null.

Hello,

I am trying to instantiate a prefab from resources folder using the name from a text file located in my resources folder. Prefab is also located in my resources folder. However, I get an error saying "the object I want to instantiate is null".

When I directly give the name of prefab it works. I cannot seem to understand the issue.

My code is :

public void Loadd() {

     TextAsset txtasset = (TextAsset)Resources.Load (txtfile);
     txtcontents = txtasset.text;
     string[] linesinfile = txtasset.text.Split ('\n');

     for(int i= 0; i<1; i++){

         for (int x = 0; x < buttonholder.Length; x++) {
             //GameObject Instance = Instantiate (Resources.Load ("F260001", typeof(GameObject))) as GameObject;   // works

             string name = linesinfile [x];
             Debug.Log (name);

             GameObject Instance = Instantiate (Resources.Load (name, typeof(GameObject))) as GameObject;

         }
     }
 }
Comment
Add comment · Show 6
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 hexagonius · Feb 08, 2017 at 12:21 PM 0
Share

if the log right before the error looks correct, there are spaces before or after the string

avatar image neosca hexagonius · Feb 08, 2017 at 12:41 PM 0
Share

The log does come right, I have checked the text file, there are no spaces before and after.

I am attaching a image of my text file. Each object name is in a new line. alt text

snip-20170208181032.png (6.7 kB)
avatar image corpsinheretoo · Feb 09, 2017 at 01:30 PM 0
Share

Some text apps add a new line and a carriage return character. You might want to do an explicit check to see that linesinfile [0] == "F260001".

avatar image neosca corpsinheretoo · Feb 10, 2017 at 05:20 AM 0
Share

It seems you are correct. I added a check if (linesinfile [0] == "F260001") before instantiation, now there are no errors since condition comes to false. However, I am confused with the Debug.log because both Debug.log(name) and Debug.log(linesinfile[o]) shows the same thing in console.

I am attaching screenshots. **alt text

snip-20170210104513.png (25.1 kB)
snip-20170210104528.png (5.5 kB)
avatar image neosca corpsinheretoo · Feb 10, 2017 at 05:39 AM 0
Share

I also tried to check if the length of both strings name and linesinfile[0] are different. However, they show to be of the same length. I am attaching screenshots of code and Debug.log .alt text

snip-20170210110442.png (24.7 kB)
snip-20170210110425.png (8.6 kB)
avatar image corpsinheretoo neosca · Feb 10, 2017 at 05:52 AM 0
Share

I think special characters (like \n, \r, \t) do not count towards length.

1 Reply

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

Answer by corpsinheretoo · Feb 10, 2017 at 05:48 AM

If linesinfile [0] == "F260001" is false and yet they look the same in the console then there is something different about the strings that is not showing up in console. As @hexagonius suggested that might be a space; however you checked for that. You split the file string on \n but some text editors add \r\n when you press return. So the string you thought looked like this: F26001\nF26002\nF26003 Really looks like this: F26001\r\nF26002\r\nF26003

Try using a different text editor (and checking the invisible characters) or set up your textfile with a different separator; e.g.: F26001;F26002;F26003 Then use: txtasset.text.Split (';');

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 neosca · Feb 10, 2017 at 06:28 AM 0
Share

You were right, it was an issue with the text editor. I used your approach and ins$$anonymous$$d of splitting with \n, used ';' and it worked. Thanks a lot.

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

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

checking if gameobject exists 1 Answer

How to Serialize or Save a List of GameObjects 1 Answer

Unnamed gameobjects appear in Unity4 when opening Unity5 project 0 Answers

Set Click area in Game 1 Answer

Accessing values on a gameobject 1 Answer


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