Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 11 Next capture
2021 2022 2023
1 capture
11 Jun 22 - 11 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 Ryanmstc · Jul 31, 2015 at 01:00 AM · 2dinstantiatecomponentclone

Instantiated GameObject's components are disabled

After I instantiated a Game Object (Sphere), it receives a new name (Sphere (Clone)) and all of its components are disabled except for the mesh renderer.

 using UnityEngine;
 using System.Collections;
 
 public class CollisionAndResetSystem : MonoBehaviour {
     int score = 0;
     public GameObject Sphere;
     public Transform sphere;
 
     void OnCollisionEnter2D(Collision2D coll) {
         if (coll.gameObject.tag == "Player") {
             print ("hit");
             Application.LoadLevel (0);
             print(score);
         }
             if (coll.gameObject.tag == "Wall") {
             print ("jumped over");
             score++;
             print(score);
             Respawn ();
 
         }
         
     }
 
     void Respawn(){ //Respawns the sphere
         Destroy(gameObject);
         Instantiate(Sphere, new Vector3(9, 1, 0), Quaternion.identity);
         Sphere.name = "Sphere"; //Here is where i tried to fix the problem with the name "Sphere" turning into "Sphere(Clone)"
 
     }
     
 }
 

How do I go about instantiating Sphere while keeping all of its components activated, or rather activating its components in the script?

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

3 Replies

· Add your reply
  • Sort: 
avatar image
1

Answer by NeverHopeless · Jul 31, 2015 at 06:12 AM

Drag drop your prefab to scene and in the inspector make sure your prefab have all its childs active and press Apply. Then try re-spawning like this:

  GameObject sphereInstance;

  void Respawn(){ //Respawns the sphere


      // Destroy the currently allocated sphere, so re-spawn can be done.
      Destroy(sphereInstance); 

      sphereInstance = (GameObject)Instantiate(Sphere, new Vector3(9, 1, 0), Quaternion.identity);

      // Change name of the instance instead of Prefab itself
      sphereInstance.name = "Sphere"; 
  }

Comment
Add comment · Show 6 · 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 Ryanmstc · Jul 31, 2015 at 08:41 AM 0
Share

I got a compile error saying "Cannot implicitly convert 'UnityEngine.Objecy' to 'UnityEngine.GameObject'. An Explicit conversion exists (are you missing a cast?)"

avatar image NeverHopeless · Jul 31, 2015 at 09:11 AM 0
Share

I missed the typecast to (GameObject). Please try now.

avatar image Ryanmstc · Jul 31, 2015 at 06:24 PM 0
Share

Im very sorry. I am new to this. where would the typecast fit and how would it look?

avatar image NeverHopeless · Jul 31, 2015 at 06:58 PM 0
Share

Check the edited code i was talking about line 2. i have already fixed that. try now.

avatar image Ryanmstc · Jul 31, 2015 at 07:31 PM 0
Share

So I tried your new code but when it collided with 'Wall' i got an error saying "The variable of Sphere in CollisionAndResetSystem doesn't exist anymore. You probably need to reassign the Sphere variable of the 'CollisionAndResetSystem' script in the inspector."

Show more comments
avatar image
0

Answer by nullgobz · Jul 31, 2015 at 01:06 AM

Change the prefab Sphere object to whatever you want. It will be created per your definition.

Or set it in code like this:

 GameObject go = (GameObject)(Instantiate(Sphere, new Vector3(9, 1, 0), Quaternion.identity));
 go.GetComponent<SomeComponent>().enable = true;

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
0

Answer by rohankad · Jul 31, 2015 at 04:58 AM

Enable the Sphere in your scene, apply changes to the prefab. Then disable it. Don't disable and apply the changes. If you do, it will be disabled by default.

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

25 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

Related Questions

2D Animation does not start 1 Answer

Changing a variable on an instantiated prefab clone once created 1 Answer

Prefab clones behaviour 0 Answers

How do I make a clone of a prefab appear on the correct layer? [5.2.2f1] 1 Answer

The variable othertransform of Prefab has not been assigned 2 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