- Home /
[Solved]Instantiating prefab from a script and destroy it from another one
Hi there,
After reading many and many docs and questions here, I couldn't make my script works.
Here is the thing :
I have an empty gameObject with a script named "GameStart.js" this script instancies some prefabs :
var Town : Transform;
var Clouds : Transform;
function Start() {
GameObject theTown = (GameObject)Instantiate(Town, Vector3 (0, 0, 0), Quaternion.identity);
Instantiate(Clouds, Vector3 (750, 1000, 750), Quaternion.identity);
}
And a second empty gameObject with a script "world.js" where I need to destroy the "Town" prefab.
So here is the question, after trying many scripts from threads where they tried to do the same thing that I want to do, how do I do this thing ?
How do I proprely do to instanciate a prefab in a script and destroying it in another one ?
Thanks you for your responses.
Answer by jnc1 · Feb 07, 2014 at 10:52 AM
Hello hello,
I finally resolved the problem and this works really fine, thanks you rutter for the help, here is how you need to do for instantiate a prefab in a script and destroying it in an other script :
firstScript.js
var Cube : Transform; // This is the variable where you specify the prefab to instantiate (in unity with drag and drop)
var theCube : GameObject; // Name of variable that will be used to destroy
function Start () {
theCube = Instantiate(Cube, Vector3(0, 0, 5), Quaternion.identity).gameObject; // Instantiate the prefab and put the gameObject into "theCube"
}
secondScript.js
var firstscript : firstScript; // "firstscript" is a variable name of your choice and "firstScript" is your script name
function Update () {
if (Input.GetMouseButton(0)) {
firstscript = GameObject.Find("EmptyGameObject").GetComponent(firstScript); // Here we get the script component into the Empty GameObject I created (manually)
Destroy(firstscript.theCube); // then call "firstscript" dot your variable name of the clone prefab
}
}
The game hierarchy looks like this in this example :
EmptyGameObject wich contains as a component the script "firstScript.js"
RandomEmptyGameObject wich contains the "secondScript.js"
Main Camera
I hope this will help people.
Answer by rutter · Feb 05, 2014 at 11:46 PM
I see three problems:
This looks like JavaScript, but you're declaring theTown
in a C# style. Does this even compile, as-is?
You're calling Instantiate on a Transform, so it will return a Transform. Your cast operation will compile, but won't run properly.
Something like this might work, instead:
var theTown : Transform = Instantiate(Town, Vector3.zero, Quaternion.identity);
Anyway, the immediate problem is that you're storing a reference to theTown
, but not keeping it. As soon as the Start()
function ends, the variable is lost. To keep the variable, you'll need to declare it at a higher scope.
So, something like this:
var Town : Transform;
var theTown : Transform;
function Start() {
theTown = Instantiate(Town, Vector3.zero, Quaternion.identity);
}
Now, all of this could still fail if your other script tries to access theTown
before it's set. Accessing other scripts during Start()
is prone to errors if you're not careful. If you read the variable, later, this is less of a problem.
Thanks for your reply,
So my first problem was to put C# code with javascript XD. Ouch, I don't know C# and that why I began to code in JavaScript.
So like this I can get "theTown" variable from an other script by using getComponent or by declaring the script, sort of an import :
var world : World;
Something like this and then do a :
destroy(world.theTown);
If I well understood the docs, I'll try all of this tonight and thanks you for clearing my $$anonymous$$d.
Edit : about the use of the variable, this is not a problem, the destroy method is called only by the action of the user
Hello again,
I wrote what you told me and that is working much better, now the another problem is that he says that I'm not not allowed to destroy the transform component, here is the actual code :
var gamestart : GameStart;
function Start() {
gamestart = GameObject.Find("GameStart").GetComponent(GameStart);
Destroy(gamestart.theTown);
}
And then if I try to do a "Destroy(gamestart)" only, there is no errors messages but nothing is destroyed (game objects).
I don't understand all of that :(
That would be great if you could help me again, thanks.
Your answer
Follow this Question
Related Questions
Access prefab's variables after instantiation 1 Answer
Instantiate A Prefab 1 Answer
How to create GameObject without adding it to scene? 1 Answer
What is the type of my prefab? 2 Answers