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 jacksilas · May 12, 2014 at 12:30 PM · instantiategameobjects

How do I instantiate gameObjects?

Hi guys, New Unity user here and also new to Programming in it... Thanks for taking the time to read this :) I have totally no idea if my code is right but basically I'm trying to create an object to appear when I click my mouse - and I want it to appear where I click. After researching a while this is what I eventually came up with:

 #pragma strict
  var camper : GameObject;
  function Update () {
         if (Input.GetMouseButtonDown(0)) {
         var mousePos : Vector3 = Input.mousePosition;
         mousePos.z = 49.9f;
         mousePos = Camera.main.ScreenToWorldPoint(mousePos);
 
 
      Instantiate(camper, mousePos, Quaternion.identity);
     
     }
     }

Could someone verify that this code is right and if not, help me out a little? What this has done for me is when I'm in Play Mode, if 'var camper' is set to be an Asset, it works! But I don't want it to be an Asset because the only ones I have are construction worker and character controller which means when I build it, it takes me into that view. When I set it to a Game Object such as a cylinder, click... nothing happens...

So all that leads to my big question: Can I somehow make a game object appear when I click? Or could I make an asset that is a game object? What would actually be the best is if I click and just a small black 2d square appears onto my sphere where I click- but I don't know how to do this and if not possible it's ok.

I would really be appreciative if a Unity Pro or anyone who has more experience with unity and programming than me could help me. Any suggestions to improve my code would be very appreciated and any help would be awesome.

Thanks a heaps guys

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 thinkwayne · May 12, 2014 at 02:47 PM

If you want a small black 2d square to appear, I would suggest making a quick 2-poly squad in your modeller, import it into your project view and give it a black material.

You then link this asset to the camper slot in this script from the inspector.

If you've created a cylinder through Unity it exists only in the scene view not the project view which is probably why you cannot instantiate it.

Remember assets are just collections of meshes, animations, textures etc, once they are in the game worlds they become game objects that contain the contents of those assets as components.

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 jacksilas · May 13, 2014 at 03:08 AM 0
Share

It works now thanks :) Somehow after writing this I came back to my game and it worked with a cube. Is there a way to make this process work with another camera? So in my game I press a button that allows my cameras to switch. But when i'm in any other camera except my first, it does not instantiate my black square. Thanks for your help

avatar image thinkwayne · May 13, 2014 at 12:54 PM 0
Share

I've never done anything that involves switching cameras, but your first camera is always tagged main camera and is accessible statically through Camera.main, you can always check if your current camera is your main camera before deciding to put something down

avatar image
0

Answer by shraa1 · May 13, 2014 at 03:24 AM

you can create a simple prefab of an asset you want to create...

then create a public GameObject object and assign the prefab to the script(in the properties)...

then you can call instantiate the GameObject object that you had created, as many times as you'd like...

I'm a novice myself, but i think this should work :)

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

22 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

Related Questions

A node in a childnode? 1 Answer

How can I assign a clone with a script to a variable? without drag & drop 1 Answer

What should I change in this instantiating a GameObject in a script using Unity? 1 Answer

How to Instantiate Objects based on a list of integers 0 Answers

Photon Instantiate doesn't always work 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