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 /
  • Help Room /
avatar image
0
Question by adhamyasser · Sep 09, 2015 at 11:00 AM · instantiatespawnnot working

gameobject doesn't spawn in the assigned random positions?

In my game I have a game object that is supposed of be transporting in random positions but it is instantiating in the empty game object that has the script this is my code ` using UnityEngine; using System.Collections;

public class thingSpawner : MonoBehaviour {

 public GameObject thing;
 public float MaxObjectCount = 0;

 // Use this for initialization
 void Start () {
     StartCoroutine (TheSpawn ());
     thing.SetActive (true);
 }
 
 // Update is called once per frame
 void Update () {
 
 }

 IEnumerator TheSpawn()
 {
     while (true) {

         yield return new WaitForSeconds(Random.Range(1,6));

         
         thing.transform.position = new Vector3(Random.Range(-7f,5f),Random.Range(-4f,4f),Random.Range(0.7f,7f));
         GameObject clone = (GameObject) Instantiate(thing, transform.position,Quaternion.identity);

         Destroy(thing,2f);

             }
 

 }

}

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
0

Answer by Peaj · Sep 09, 2015 at 11:19 AM

It seems like you are moving your "thing" and then destroy it. But the new thing you instantiate gets the position of the object the script is attached to and not the position of your moved old thing. The position of "this" gameobject didn't change. So depending on your intention (if you want to move or delete/recreate the object) you could either just change the transform.position parameter of instantiate to thing.transform.position or just delete your Instantiate and Destroy lines.

Comment
Add comment · Show 3 · 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 adhamyasser · Sep 09, 2015 at 11:23 AM 0
Share

but if I delete the instantiate line no game object will be created correct??? @Peaj

avatar image Peaj · Sep 09, 2015 at 12:26 PM 0
Share

It depends on what you want. You could either just move the existing object or destroy it and instantiate it in a new position.

What you do right now is:

-$$anonymous$$ove the old object

-Instantiate a new one at the wrong position (not your random position)

-Delete the old one

If you just want to move the object around I would recommend to just move it and get rid of the cloning/deleting code.

avatar image Peaj · Sep 09, 2015 at 12:34 PM 0
Share

If you want to stay with cloning the object (however it isn't necessary in most situations) just change the line "GameObject clone = (GameObject) Instantiate(thing, transform.position,Quaternion.identity);" to "GameObject clone = (GameObject) Instantiate(thing, thing.transform.position,Quaternion.identity);"

avatar image
0

Answer by IHackedDeath · Sep 09, 2015 at 11:21 AM

Hi adhamyasser,

I have read through your question and I am not entirely sure on what you are asking?

I assume you want the object to continuously teleport to new random positions.

If so then I would recommend moving start coroutine (Thespawn()) to update instead of start as in the start function it is only called once.

Also I would recommend the same thing for where you change thing.setactive to move it into update.

If you would like to give a bit more information on what exactly you are after and maybe an example then I am happy to assist further.

I hope this helps.

Kind Regards,

IHackedDeath.

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 adhamyasser · Sep 09, 2015 at 11:32 AM 0
Share

it was still instantiated in the same place and the instantiated object (thing) instantiated even faster any other ideas ??? @IHackedDeath

avatar image Peaj · Sep 09, 2015 at 12:30 PM 0
Share

You should not start a coroutine in the Update method. StartCoroutine already makes sure that the function is called every frame. So Start is totally fine.

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

30 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

Related Questions

Instantiate not spawning anything 1 Answer

Random position of asteroids -- Random not randomizing? 1 Answer

Instantiate a prefab at three specific x positions 0 Answers

Trying to spawn enemies on only one path 1 Answer

Networking - How do i spawn an object with client authority? 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