Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 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
1
Question by gaurit · Oct 12, 2015 at 02:31 PM · gameobjectcloneinstantiation

scripts gets disabled when the prefab is instantiated

I have two script named inst and coll and two gameObject , one is empty and the other is the instance of prefab named Car . inst script is attached to the empty gameObject and its function is to destroy the car gameObject when touched and instantiate the car gameObject to the touch position.

coll script is attached to the car prefab , and its function is to detect collision.

script is working absolutely fine but a problem occurs. Whenever the instance is created of the car gameObject ,when touched ; The coll script attached to it get disabled in the created instance(clone) of the car.

I searched many sites , but was not able to clearly know the problem's solution..

Here is my Inst script code

 using UnityEngine;
 using System.Collections;
 
 public class inst : MonoBehaviour {
 
     Ray ray;
     RaycastHit hit;
     public GameObject gObj;
     public GameObject gNew;
 
 
     void Start () {
         gObj = GameObject.FindGameObjectWithTag("Player");
         gNew = GameObject.FindGameObjectWithTag("Player");
     }
     
     // Update is called once per frame
     void Update () {
         CreateInstance();
         
             }
     
     void CreateInstance (){
     
         if (Input.touchCount > 0 && Input.GetTouch(0).phase == TouchPhase.Began){
         
             
             ray = Camera.main.ScreenPointToRay(Input.GetTouch(0).position);
 
             Debug.DrawRay(ray.origin, ray.direction * 20);
             //Debug.Log(Physics.Raycast(ray,out hit, Mathf.Infinity));
             
             if (Physics.Raycast(ray,out hit, 20f))
                 
             
             {
                 
             
                 CreatePrefab();
             }
 
                 
                 }
                 
             }
         }
     
     }
     
     private void CreatePrefab() {
 
         gObj = gNew;
         if (gObj != null ){
             Destroy(gObj);
             gNew = Instantiate(gNew,hit.point, transform.rotation) as GameObject ;
         }
 
     }
     
 }
 







Comment
Add comment · Show 6
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 rockyourteeth · Oct 13, 2015 at 04:52 AM 0
Share

I think you need to try to make this more clear. Your CreatePrefab() function is really confusing. Can you add some comments to the script to explain why you are doing each line? What is the purpose of gObj and gNew? Why are you setting it, and then immediately destroying it in CreatePrefab()?

avatar image gaurit · Oct 13, 2015 at 05:03 AM 0
Share

createPrefab fn is used to assign the new gameobject to the old gameobject and destroy the old gameobject and instantiate the new at its place..

gObj holds the old gameobject and gNew holds the new gameObject.

Basically, i want to destroy the gameobject on touch and instantiate the same gameobject in the touch position..

avatar image rockyourteeth · Oct 13, 2015 at 02:12 PM 0
Share

It looks like you aren't referencing any prefab. In your start function, you're putting the player object in the scene into gObj and gNew. So, you now have no reference to any prefab that you might have had. All you're doing is duplicating the object in the scene, not instantiating from a prefab.

avatar image gaurit rockyourteeth · Oct 13, 2015 at 02:35 PM 0
Share

Thanks, but could you explain me a more what did you mean by duplicating the object and not instantiating from prefab . Didn't Instantiating prefab, duplicates prefab in the scene and also when you instantiate a prefab , you can clearly see the prefab name changes into prefab(clone)..

Also can you give me the solution, if I am wrong and how could i have done better..

avatar image gaurit gaurit · Oct 13, 2015 at 03:13 PM 0
Share

Thanks buddy.. you sorted out my confusion ..

Show more comments

1 Reply

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

Answer by rockyourteeth · Oct 13, 2015 at 03:09 PM

First of all, terminology is confusing, so let's make sure we're on the same page. A "prefab" is an object which is not in the scene, but is ready to be created, or, instantiated into the scene. You have to keep a reference to that prefab through a variable that you set in the editor before you run the game. An "instance" of a prefab is one that you have instantiated, or created, in the scene. It is now an object in the scene, an instance of the prefab, but not a prefab anymore. But you should still keep your reference to the original prefab, so you can create more if you want to.

If you create an instance of a prefab, and then continue to duplicate the instance after that, instead of the prefab, you're just making copies of copies, instead of making a bunch of copies from the original. This could result in unexpected behavior.

You probably want to create a "public GameObject CarPrefab", or something like that, and place the prefab into that in the editor before you run the game. Then, during the game, you don't want to set "CarPrefab" as anything new. Just leave that as the prefab reference, and use that when you instantiate:

 gNew = Instantiate(CarPrefab, hit.point, transform.rotation) as GameObject ;

Now, gNew is always the new object instance, but CarPrefab always remains as a reference to the prefab.

And in this case, you should need "gObj" and "gNew". Just use one of them. You can Destroy gObj before you instantiate a new object, and then set gObj as that new instance. Like this:

 if (gObj != null )
 {
   Destroy(gObj);
 }
 gObj = Instantiate(CarPrefab, hit.point, transform.rotation) as GameObject ;

Hope that helps.

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

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

33 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 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

GetComponent of ALL clones? 2 Answers

Why do some objects freeze on creation? 1 Answer

Instantiation and Deletion very slow... HELP.. 3 Answers

How can I destroy an specific clone already instantiated, when there's more than one? 1 Answer

save/load cloned game object 1 Answer


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