Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 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 tomowale · Mar 12, 2015 at 02:41 PM · spawnspawningcube

Spawning Cube on empty gameobject

Ok I've had this problem for a while now, basically I am trying to spawn a cube prefab on a empty game object.Here is my script.

 using UnityEngine;
 using System.Collections;
 
 public class Spawner : MonoBehaviour{
 
      public GameObject Cube; 
     public float spawnTimeDelay = 3f;
     public float spawnTimeInGame = 2f;
     public Vector3 spawnLocation = new Vector3 (0,2,0);
 
     void Spawn () 
     {
         GameObject SpawnLocation = (GameObject) Instantiate ( Cube, spawnLocation, Quaternion.identity);
     }
 
 
 
     
 
         
         
     // Use this for initialization
     void Start  ()
     {
         InvokeRepeating ("Spawn", spawnTimeDelay, spawnTimeInGame);
     }
 }
 
 
Comment
Add comment · Show 23
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 AlwaysSunny · Mar 12, 2015 at 04:47 AM 0
Share

Not enough information. Please tell what-it-does as well as what-you-want in a sensible explanation. In what way does this not work?

avatar image tomowale · Mar 12, 2015 at 05:10 AM 0
Share

I created empty game objects to act as spawn locations for the cubes, because I am prototyping a car game where basically cars are supposed to spawn one after another an act as cars. I just want the cubes to move forward to the player after it is spawned. Also It is not working because it refuses to spawn the cube or anything. Hope that helped if , you need more explanation , I will give it to you.

Thanks

avatar image AlwaysSunny · Mar 12, 2015 at 05:19 AM 0
Share

How about

 GameObject go = GameObject.Instantiate(Cube, spawnLocation, Quaternion.identity) as GameObject;

Since you're not doing anything with the variable, it should be sufficient to just

 GameObject.Instantiate(Cube, spawnLocation, Quaternion.identity);
avatar image tomowale · Mar 12, 2015 at 01:52 PM 0
Share

I've changed it , Unity is saying that the variable cube of Spawner hasn't been assigned?

avatar image AlwaysSunny · Mar 12, 2015 at 04:25 PM 0
Share

Assign a game object to the Cube variable to this script instance in the inspector. This object should be a prefab.

Show more comments

1 Reply

· Add your reply
  • Sort: 
avatar image
1

Answer by melwei · Mar 14, 2015 at 10:48 PM

Try it with:

 cube=GameObject.CreatePrimitive(PrimitiveType.Cube);
 cube.transform.position = new Vector3(transform.position.x,transform.position.y,transform.position.z);






Comment
Add comment · Show 12 · 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 tomowale · Mar 15, 2015 at 05:08 AM 0
Share

Sorry for posting as an answer , but anyway melwei I did it , all errors are gone now only thing is the cube doesn't appear so you probably fixed my issue , but I might have done something wrong.I'll give you a print screen. The cubes appear in the hierarchy but not in the game when it is being run. alt text

image5.png (119.5 kB)
avatar image melwei · Mar 15, 2015 at 08:20 AM 0
Share

I think you have to place the empty gameobject at another position because the script part I wrote will place a cube at the position of the empty gameobject. If you dont want the cube to spawn at the position of the empty gameobject, you have to put numbers into the Vector3- like this: cube.transform.position = new Vector3(0,2,0)

avatar image melwei · Mar 15, 2015 at 08:25 AM 0
Share

Here is a part of a script I used.

GameObject cube = GameObject.CreatePrimitive(PrimitiveType.Cube); cube.transform.position = new Vector3(0,-7,0);

It really works. $$anonymous$$aybe you just cant see the cube because it has the same texture like your background.

avatar image tomowale · Mar 15, 2015 at 03:01 PM 0
Share

Bruh , melwei I think you have fixed my problem I a deleting the empty game objects and will just let the cubes spawn at positions in Vetor3 like you said. I am trying to change the color of the cube so I can see it do you know how?

avatar image tomowale · Mar 15, 2015 at 07:23 PM 0
Share

By the way does that script spawn the object repeatedly or just once?

Show more comments

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

6 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

How to make an object spawn in different positions? 2 Answers

spawning game objects 1 Answer

[Help] How Do I Randomly Spawn Game Objects On Specific Coordinates? 3 Answers

Spawning won't work 1 Answer

Cannot acces other scipt 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