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 Bee_Bone · Apr 17, 2018 at 07:20 AM · instantiategameobjectsspawningclones

extra GameObjects Spawning

The script below Spawns Ghosts at random positions every 10 seconds:

 using System.Collections;
 using System.Collections.Generic;
 using UnityEngine;
 
 public class GhostSpawner : MonoBehaviour {
 
 float timer;
 public List<GameObject> Ghosts;
     
     // Update is called once per frame
     void Update () {
         timer += Time.deltaTime;
         if(timer > 10){
             Instantiate(Ghosts[Random.Range(0,4)], transform.position = new Vector3(Random.Range(-20,20), 1, Random.Range(-20,20)), transform.rotation);
             timer = 0;
         }
     }
 }


This Script Instantiates a set of 6 static GameObjects (PictureFrames) at runtime:

 public bool Start() {
     GameObject PictureFrame = Resources.Load("Picture") as GameObject;    
      GameObject Frame = Instantiate(PictureFrame, transform.position, transform.rotation);
     Frame.transform.parent = gameObject.transform;
 }


The problem is that each time the GhostSpawner spawns, one of the six PictureFrame produces a clone as well (with the same transform).

It is always the same one, and the clone always instantiates at the same time as the spawner (which is what leads me to believe this is the cause of the problem).

Does anyone have any idea why this is happening and how I could stop it?

Comment
Add comment · Show 1
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 shadowpuppet · Apr 17, 2018 at 06:56 PM 1
Share

I'm confused by "public bool Start(){". where is this picture frame script? is it on the Ghost prefabs? If so that would explain why a picture frame is spawning with a ghost in the same spot because you're telling the picture frame transform to match the transform of the object the script is on. I think

2 Replies

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

Answer by bunnynsnake · Apr 17, 2018 at 07:01 PM

you should try change this part of the code to public void Start() { GameObject PictureFrame = Resources.Load("Picture") as GameObject;
GameObject Frame = Instantiate(PictureFrame, transform.position, transform.rotation); Frame.transform.parent = gameObject.transform; }

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 Bee_Bone · Apr 19, 2018 at 09:41 AM

You'v Done it!

Thanks a lot @bunnynsnake.

I had it as a bool because another script was running a yield that waited for the Start method to return true.

But've I've just added a bool at the bottom instead and it's all working well.

Cheers again!

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

99 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

Related Questions

How to compare the positions of two or more instantiated gameobjects (clones) with different tags? 1 Answer

how do I reference a clones of a instantiated game object and only the clones In a script? 1 Answer

Instantiate creates multiple clones. 2 Answers

Problems with spawning items that the players can pick up - Unet 0 Answers

Spawning multiple objects in same area without intersecting each other 0 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