• 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 bwolf2 · Sep 23, 2016 at 11:41 AM · generationside scroller

How do I generate obstacles in 2D side scroller?

I am building an endless 2D side scroller and I have created an object that will end the game when the player collides with it. The question I have, however, is how to have obstacles generate with a random frequency, and come at the player on one axis. Here is my code so far

 using UnityEngine;
 using System.Collections;
 
 public class obstacleScript : MonoBehaviour
 {
 
     // Use t$$anonymous$$s for initialization
     void Start()
     {
         
     }
 
     float moveSpeed = 2.5f;
 
     public static Object Instantiate(Object original, Vector3 position, Quaternion rotation);
 
 
     // Update is called once per frame
     void Update()
     {
         //makes obstacle move towards player
         transform.Translate(Vector3.left * moveSpeed * Time.deltaTime);
     }
         
     //Detects if player $$anonymous$$ts an obstacle
     void OnTriggerEnter2D(Collider2D other)
     {
         //If a gameObject with the tag "Player" enters t$$anonymous$$s trigger, load a scene.
         if (other.gameObject.CompareTag("Player"))
         {
             print("object $$anonymous$$t");
             Application.LoadLevel("Game Over Scene");
         }
     }
 }  

Currently, the error I am getting is that I have to attach a body to my instantiate line.

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 Happy-Zomby · Sep 23, 2016 at 06:08 PM

you can instantiate in the declaration part... what you want to do is start a coRoutine w$$anonymous$$ch will wait a random number of seconds and then instantiate the obstacle there...

For coroutine and waiting see here: https://docs.unity3d.com/ScriptReference/WaitForSeconds.html

for instantiate see here: https://docs.unity3d.com/ScriptReference/Object.Instantiate.html

also you seem to be missing 2d and vector3... t$$anonymous$$s may cause issues I believe - i work in 3d but i guess 2d should use vector2 not 3.

hope that helps,

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 bwolf2 · Sep 23, 2016 at 07:29 PM 0
Share
avatar image Happy-Zomby bwolf2 · Sep 23, 2016 at 07:34 PM 0
Share
avatar image
0

Answer by Badeye · Sep 23, 2016 at 06:43 PM

Instantiate in a void and make sure the GameObject you try to instantiate is not null (create a public GameObject and select a Prefab in the Inspector, maybe perform a null check). Also make sure the wall-object is set to trigger, otherwise OnTriggerEnter will not get called.

Comment
Add comment · Show 1 · 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 bwolf2 · Sep 23, 2016 at 08:11 PM 0
Share

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.

Welcome to Unity Answers

If you’re new to Unity Answers, please check our User Guide to help you navigate through our website and refer to our FAQ for more information.

Before posting, make sure to check out our Knowledge Base for commonly asked Unity questions.

Check our Moderator Guidelines if you’re a new moderator and want to work together in an effort to improve Unity Answers and support our users.

Follow this Question

Answers Answers and Comments

5 People are following this question.

avatar image avatar image avatar image avatar image avatar image

Related Questions

Raycasting plane mesh from the side 0 Answers

Set procedurally generated objects as static 1 Answer

transform.Rotate(Vector3.up ... not working 0 Answers

How many iPhone users have first gen devices? 1 Answer

Instantiate GameObject on local axis rather than world axis 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