• 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
Question by connie · Nov 01, 2011 at 10:50 PM · instantiatespawn

spawn random objects in multiple position

hi all. i am making a puzzle game. and there are 4 position in the game that the pipe appear randomly at first. And when the pipe got dragged out, i want to make a new pipe instantiate at that position. :)

i have done the code for the pipes instantiating random at first, click and drag object. but i don't know how to make it appear randomly again at the position after the pipe got dragged out :(. HELP!!

Here is the full script. Thank :)

 var blockTypes: GameObject[];
 var inGamePos: Array = new Vector3[4];
 var pos1: GameObject;
 var pos2: GameObject;
 var pos3: GameObject;
 var pos4: GameObject;
 static var clickedObj : Transform; 
 private var offSet : Vector3;     
 var cubewall : GameObject;
 private var snapAllowance: int = 3;
 var blockTypesWeight= [1,1,3,3,1,1];
 
 
 
 function Start () {
 
     inGamePos[0] = pos1.transform.position;
     inGamePos[1] = pos2.transform.position;
     inGamePos[2] = pos3.transform.position;
     inGamePos[3] = pos4.transform.position;
 
 
     for (i = inGamePos.length - 1; i >= 0; i--) {
        var ranPipe: GameObject = blockTypes[Random.Range(0,blockTypes.length)];
        var newPipe: GameObject = Instantiate(ranPipe,inGamePos[i],Quaternion.identity);
         newPipe.name = "Block"; 
            }
 }
 
 
 function Update () {
     var ray = camera.ScreenPointToRay(Input.mousePosition);     
     if (Input.GetMouseButtonDown(0)) {    
        var hit : RaycastHit; 
        if (Physics.Raycast(ray, hit, Mathf.Infinity)) {      
          if(hit.transform.gameObject.tag == "Pipe") {
           clickedObj = hit.transform;    
           offSet = clickedObj.position-ray.origin;   
          }
 
 
        }
     }
     else if (Input.GetMouseButtonUp(0)) {
 
          if(clickedObj != null&& clickedObj.GetComponent(BlockScript).hitObj != null){
           if(clickedObj.gameObject.transform.position.x>=clickedObj.GetComponent(BlockScript).hitObj.transform.position.x-snapAllowance && clickedObj.gameObject.transform.position.x<=clickedObj.GetComponent(BlockScript).hitObj.transform.position.x+snapAllowance)
          snap(clickedObj.gameObject,clickedObj.GetComponent(BlockScript).hitObj.transform.position);
 
          clickedObj = null;
 
        }   
 
     }
     if (clickedObj) {
        clickedObj.position = Vector3(ray.origin.x+offSet.x, ray.origin.y+offSet.y,clickedObj.position.z);     
 
     }
 
 }
 
 
 function snap (objToSnap: GameObject, posToSnap: Vector3) {
 
     print("snap object");
     objToSnap.transform.position.x = posToSnap.x;
     objToSnap.transform.position.y = posToSnap.y;
     clickedObj.GetComponent(BlockScript).hitObj = null;
     Spawn();
 
     }
 
 
 function Spawn (// i think i need to put something here ??){
 
        var ranPipe: GameObject = blockTypes[Random.Range(0,blockTypes.length)];
        var newPipe: GameObject = Instantiate(ranPipe,inGamePos[0],Quaternion.identity);
 
 
      }
Comment

People who like this

0 Show 0
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

Answer by WillTAtl · Nov 02, 2011 at 12:36 AM

Simplest way with the code you have now would be to add a private variable, drugFromPosition : vector3, which you set to the position of clickedObj right after you assign clickedObj to the hit object. Then, in Spawn, instead of a position from inGamePos, you can simply instantiate a new pipe at drugFromPosition.

That's it for directly solving your problem, but I would like to add, there are many things that could improve the structure of this code. I won't go into it completely, just point out one big one. A lot of this code could be moved into a script that is attached to the pipes themselves (it seems to be on some sort of game manager script now). If restructured this way, you could eliminate a lot of your code in Update, just using the OnMouseDown and OnMouseUp events of the pipe script instead. The system would then handle the collision testing for you, and you could focus on the desired behavior and eliminate a lot of code!

There's a bit more to the restructuring than that, but ultimately you could cut the amount of code necessary to achieve the same results in half - or more - by taking full advantage of the things Unity can do for you, instead of doing it all yourself!

Hope this helps!

Comment

People who like this

0 Show 0 · 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

Answer by connie · Nov 02, 2011 at 05:23 AM

and how should i make the code that the new pipe can replace the old pipe in cubewall? :( thank.

Comment

People who like this

0 Show 0 · 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

Answer by connie · Nov 02, 2011 at 05:23 AM

it worked. thank you very much! but there is problem that it not only spawn in 4 position but also spawn when in cubewall. i mean when i dragged from inGamePos to the cubewall and then click that pipe and drag it again, it instantiate in the cubewall. should i make the pipe non-clickable after place it in the cubewall? how?? Thank

Comment

People who like this

0 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 WillTAtl · Nov 02, 2011 at 02:03 PM 0
Share

I think you're misunderstanding the purpose of unityAnswers a bit. Did you even try to solve this next problem yourself before returning to ask for a solution? We've all got code to write for our own game projects, too! :)

Just think it through. You've got some code to drag and drop from sources to the well, which works as intended. You want that code to stop running when they've been dropped in the well. Code running in certain cases, not in others - that sounds like a conditional (if) to me!

Just keep hacking at the code, it'll get easier with practice!

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

3 People are following this question.

avatar image avatar image avatar image

Related Questions

Instantiating prefab at child (spawnlocations are arrays) 2 Answers

instanstiate Spawns more then one objec 0 Answers

when i Instantiate a bullet i can"t control the spawn rate. when i press mouse button down its spawn unlimited without limit 1 Answer

Network.Instantiate players at each spawnpoint? Don't use the same spawnpoint? 1 Answer

Spawn from array 2 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