- Home /
Calling an object from a different JS Script and Instantiate it
Hi everyone, I already have House_1 script in which i built a house with columns. Now i need to build another house (script: House_2) in which i 'll put it a few columns too. Instead of copy-paste the same code to build those columns, i prefer to Instantiate the column object by the previous script. try to avoid the same many code lines as i had with the House_1 script.
Here is the main idea:
Class House_1:
//code
var oColumn = GameObject.CreatePrimitive(PrimitiveType.Cube);
oColumn.transform.position = Vector3(RandX -3.2, 1.7840475, RandZ + 3.3772203);
oColumn.transform.parent = oMainBuilding.transform; //parenting
oColumn.name = "Column right 0";
oColumn.transform.localScale.x = 0.05992945;
oColumn.transform.localScale.y = 0.3776168;
oColumn.transform.localScale.z = 0.08529133;
//code
Class House_2 ( i tried 3 options, close to my problem):
(1)
`var column : House_1 = GetComponent(House_1);
column.oColumn;`
And got the exception: "'oColumn' is not a member of 'House_1'."
(2)
`var column: GameObject;
column = GameObject.Find("House_1/Column right 0");
column.transform.parent = oMainBuilding.transform; //parenting
column.transform.position = Vector3(RandX, 1.7840475, RandZ + 3.3772203);
column.name = "column";
column.transform.localScale.x = 0.05992945;
column.transform.localScale.y = 0.3776168;
column.transform.localScale.z = 0.08529133;`
No exception but i can't see anything.
(3)
`var columnObj : GameObject;
var column= GameObject.FindWithTag("House_1/Column right 0");
columnObj.transform.parent = oMainBuilding.transform; //parenting
Instantiate(columnObj, column.transform.position, column.transform.rotation);`
And got the exception: "UnityException: Tag: House_1/Column right 0 is not defined! UnityEngine.GameObject.FindWithTag (System.String tag) (at C:/BuildAgent/work/6bc5f79e0a4296d6/Runtime/ExportGenerated/Editor/BaseClass.cs:414)"
Anyone knows why? Or know what i can do to see the column in my new House? Thanks in advance.
Answer by Berenger · Jun 21, 2012 at 09:17 PM
You're not writing your entire code so it's hard to say, but apparently oColumn isn't declared in the correct scope. It should be a member of the class, not a var from the function.
How do we gain access from a member of a class? the oColumn is an object created in the House_1 class, and it's a child to o$$anonymous$$ainBuilding: (Class) House_1 { (GameObject) o$$anonymous$$ainBuilding --> (GameObject) oColumn --> (GameObject) oColumn_base --> (GameObject) oColumn_upperpart }
oColumn is not a member of a function nor is a variable. (other languages used to do that to gain access: House_1.oColumn)
Your answer
Follow this Question
Related Questions
Unity script system 2 Answers
Instantiating a class 1 Answer
Unity return error message like method not a member of Object class 1 Answer