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
0
Question by blueFire · Jun 29, 2014 at 07:54 PM · instance

Setting a variable in individual versions of an instantiated object

The goal is to simply create several instances of a prefab assigning each instance a different value in a variable contained in the prefab:

 public Transform thumDisplay;
 
 void createDisplay()
 {
     for (int a = 0; a < 2; a++)
     {
         int x = a * 2;
         int y = 0;
     string details = a.ToString();
         GameObject test;
         test = Instantiate(thumDisplay, new Vector3(x, y, 0), Quaternion.identity) as GameObject;
         test.GetComponent<thumbBehavior>().individualDetails = GetComponent<controller>().details;
     }
 }

The variable declaration in the prefab is:

 public string individualDetails;

It works perfectly if I remove the line that reads: "test.GetComponent().individualDetails = GetComponent().details;"

I get an "NullReferenceException: Object reference not set to an instance of an object" error message when I add the "test.GetComponent().individualDetails = GetComponent().details;" line.

Thanks in advance for the help.

Jason

Comment
Add comment · Show 2
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 RiQQ · Jun 30, 2014 at 01:29 AM 0
Share

First of all, have you tried assigning the local variable 'details' to test.GetComponent().individualDetails? And second, i sure hope you are not referencing in the second section "GetComponent().details" to the same script + if you would do this, in my knowledge you shouldn't be able to see this local variable 'details' from outside. But after this if it's not working, there could be something that i had when i Instantiated straight to gameObject it didn't do it correctly. Workaround for this was first to get it as Transform and then convert it to gameObject

ie.

 GameObject test;
 Transform t;
 t = (Transform) Instantiate(thumDisplay, new Vector3(x, y, 0), Quaternion.identity);
 test = t.gameObject;

avatar image blueFire · Jul 01, 2014 at 01:20 AM 0
Share

RIQQ: Your code allows me to create the object without any errors but I still cannot assign a value to the created object's variable.

I even tried the code below. It runs without any errors but the created object's variable has no value assigned to it.

 GameObject test;
 Transform t;
 t = (Transform) Instantiate(thumDisplay, new Vector3(x, y, 0), Quaternion.identity);
 test = t.gameObject;
 test.GetComponent().individualDetails = GetComponent().details;


Jason

1 Reply

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

Answer by Aeron0 · Jul 01, 2014 at 03:44 AM

I would make your GameObject a prefab first and put that prefab in a folder called Resources in your project. Then instantiate the prefab in your script.

 void createDisplay()
 {
   for (int a = 0; a < 2; a++)
   {
     int x = a * 2;
     int y = 0;
     string details = a.ToString();
 
     GameObject test;
    
    // Instantiate the prefab from Resources folder
  test = (GameObject)Instantiate(Resources.Load("myPrefab"));
 
  // The variable named "details" is just a string. Dont need to call GetComponent.
  // This code will work if individualDetails is a string variable.
  test.GetComponent<thumbBehavior>().individualDetails = details;
   }
 
 }
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 blueFire · Jul 02, 2014 at 01:24 AM 0
Share

Aeron0. This worked perfectly.

Thank You.

Jason

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

4 People are following this question.

avatar image avatar image avatar image avatar image

Related Questions

Kimmons Dialogue and JS Scripts / Player Inventory 0 Answers

Help with Error: An instance of type 'Regex' is required to access non static member 'Replace'. 2 Answers

Comparing PhysicMaterials at Runtime 3 Answers

Is there a way to override a function in each instance of the same script? 3 Answers

Using Instance Nodes, worth it? 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