- Home /
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
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;
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
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;
}
}
Your answer
Follow this Question
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