- Home /
Manipulating a prefab from within the script that instantiated it
I'm new to Unity coming from XNA, and I feel like I'm seriously missing something.
what I would like to do is have a GO instantiate a prefab, and then have access to that instance's public fields and methods (rather, the fields and methods of the script attached to the prefab). For example, if I wanted to have a "GameBoard" object instantiate a "Tile" prefab in a 3D version of a board game, and then based on some condition within the GameBoard, tell the Tile to call one of its methods. I haven't seen a way to do this on any tutorials, and I haven't seen it asked on this forum - what am I missing?
Thanks!
Answer by save · Dec 02, 2011 at 11:20 AM
There are a couple of ways of doing it, depends on what pipeline you're after. It's always easier to have an object that functions as a manager for a level or a complete game.
Declaring via static components:
var gameBoardPrefab : GameObject;
function Start () { Instantiate(gameBoardPrefab, Vector3.zero, Quaternion.identity); }
function DoSomething () { gameBoardScript.foo = "bar"; //Set foo : String in gameBoardScript to "bar" gameBoardScript.foo(); //Run function foo() in gameBoardScript }
Declaring via cached components:
var gameBoardPrefab : GameObject; static var gameBoard : GameObject; static var gBcomponent : gameBoardScript;
function Start () { gameBoard = Instantiate(gameBoardPrefab, Vector3.zero, Quaternion.identity); gBcomponent = gameBoard.GetComponent(gameBoardScript); }
function DoSomething () { gameBoard.gBcomponent.foo = "bar"; //Set foo : String in gameBoardScript to "bar" gameBoard.gBcomponent.foo(); //Run function foo() in gameBoardScript }
The same method would be working for the game board and its tiles where an array would come in handy.
tile[64].SomeFunction(SomeValue);
@akekajoo, in the second example gameBoardScript is the type of the script. This type is the script name without quotes or extension: if this script is called "gameBoardScript.js", its type will be gameBoardScript.
Answer by akekajoo · Dec 02, 2011 at 08:45 PM
Thanks for the response, though I can't seem to get this method (in particular the 2nd one) to work using C#. Here's what I did as a test:
I have GameBoardPrefab and TilePrefab, and they have attached to them GameBoardScript.cs and TileScript.cs. All TileScript has is the following (Where can I find instructions for formatting code as you did in your response, btw?):
string foo;
public void foo(Vector3 moveVector)
{
gameObject.transform.Translate(moveVector);
}
GameBoardScript has the following, which was my attempt to translate your JS example to C#:
GameObject tilePrefab;
static GameObject tile;
static TileScript tileScript;
void Start() {
tile = (GameObject)Instantiate(tilePrefab, new Vector3(5, -2, 0), Quaternion.identity);
tileScript = tile.GetComponent<TileScript>();
Visual Studio didn't like the code until I cast the instantiation as a GO, but there we are, I guess.
At this point, I tried to add "tile.tileScript.foo = "bar";" but this caused the following error (and what is the point of this foo = "bar" business anyway, if I may ask?):
Error 1 'UnityEngine.GameObject' does not contain a definition for 'tileScript' and no extension method 'tileScript' accepting a first argument of type 'UnityEngine.GameObject' could be found (are you missing a using directive or an assembly reference?)
Adding the line for calling foo() caused a duplicate error.
Thank you again!
Answer by akekajoo · Dec 03, 2011 at 11:20 PM
For closure, I figured it out, and the only problem was that I had somehow unassigned the prefab. After loading the prefab in code from a resources folder, it's all good. And embarrassingly, I didn't realize that setting the string foo to "bar" was just an example of doing something with the prefab's script, though I understood that with the foo method example.
Anyway, thanks a lot!
Your answer
Follow this Question
Related Questions
Instantiated Prefab's script doesn't work 2 Answers
How to access script variables attached to a prefab at runtime in Javascript? 2 Answers
Calling a function from a instantiated clone 1 Answer
Instantiating Prefab from Javascript - BCE0005: Unknown identifier: 'Prefab' 2 Answers
[Solved]Instantiating prefab from a script and destroy it from another one 2 Answers