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 jnc1 · Feb 05, 2014 at 11:43 PM · gameobjectinstantiateprefabvariable

[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.

Comment
Add comment
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

2 Replies

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

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.

Comment
Add comment · 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
2

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.

Comment
Add comment · Show 2 · 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 jnc1 · Feb 06, 2014 at 09:45 AM 0
Share

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

avatar image jnc1 · Feb 06, 2014 at 10:22 PM 0
Share

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

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

19 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

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

how can I display a variable as a GUIText 5 Answers


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