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
0
Question by JohnKMcCarthy · Apr 26, 2020 at 07:32 AM · instantiatecloneinstantiate prefab

Instantiate inactive,Making a GameObject active/instantiate

Noob question for my first project.

I want to make a GameObject 'bigBrother' appear when another two objects collide.

I've been trying to do this by having it inactive and using SetActive with an if statement, but I can't make it work and I realised I can't even do this without the If statement, not even with a script that says nothing else but SetActive on Start or Wake or Update... I thought that perhaps it is because Unity can't see objects that are inactive at the start of the game, and some forum posts suggest making the inactive prefab a child of an active empty (https://answers.unity.com/questions/921123/enabling-a-disabled-prefab-in-the-scene.html) but this doesn't seem to make a difference.

So I figure SetActive is not the way and I am trying out Instantiate. This works insofar as it creates a clone in the game but one that is inactive and I can't get it to activate! I have seen examples of code that just say SetActive.'name of prefab' that are supposed to activate the just instantiated clone, but maybe I need to refer to the clone specifically? The in-game name is suffixed with '(clone)'

 using System.Collections;
 using System.Collections.Generic;
 using UnityEngine;
 
 
 public class SpawnBigBrother : MonoBehaviour
 {
     public GameObject bigBrother;
         void OnCollisionEnter(Collision col)
     {
         if (col.gameObject.name == "Wall")
             {
                 Instantiate(bigBrother);
                 bigBrother.SetActive(true);
         }
     }
 }

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
2

Answer by endasil_unity · Apr 26, 2020 at 10:04 AM

Instantiate(bigBrother) creates a clone of the bigbrother object, then with bigBrother.SetActive(true); you activate whatever is your original bigBrother object, not the cloned object. Try this:

 GameObject clone = Instantiate(bigBrother) as GameObject;
 clone.SetActive(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 JohnKMcCarthy · Apr 26, 2020 at 01:09 PM

Oh yeah that looks useful as it defines the name rather than let it be auto generated - that will be much more better for my intended use.

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 ransomink · Apr 26, 2020 at 02:11 PM

Is your bigBrother game object already in the scene? If it is, then you just need to do: bigBrother.SetActive(true); when the object's collide


If you want to spawn your bigBrother object into the scene, when you instantiate, you do not save the created game object to a variable, so you cannot access it and set it to active. This will create a game object and set it to active. Instantiate also has parameters to set the position, rotation, and parent of the created game object. Add those, if needed

 GameObject clone = Instantiate(bigBrother);
 clone.SetActive(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

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

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

Instantiate Reference Problem 1 Answer

Error CS0029 - Trying to destroy an object and then instantiate it again? 1 Answer

Multiple Buildings Placement 1 Answer

Instantiate and add Rigidbody force 3 Answers

Following Object can't find newly Instantiated Object (Solved) 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