• 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
Question by VictorSIlvaK · Jul 17, 2016 at 06:37 PM · c#javascriptuimouseclickadd

How add(create) a new gameobject on scene on mouse button click.

Hey guys... so, I am really new in code world and unity, so things are yet blurred to me. In fact I am learning yet how the things unroll and happens in the coding world. I had already looked here at Unity Community as in web as well but nothing works or fit to me. What I want is after select a specific object in a list on UI, add that object to the scene I am in with mouse button click, what I mean is like we do in games like Cities Skylines, Prison Architect or other administration games we have around there where we can place objects on the world, or on the scene as I want. Well, I would appreciate any help. I would like to ask for a explanation of what is happening on the code for I really learn where it is going to. So, thanks all.

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

2 Replies

· Add your reply
  • Sort: 
avatar image

Answer by Anas173 · Jul 17, 2016 at 07:08 PM

Hello I see what u want. i Guess i can help you.

So first you need an array of your GameObjects that you want to instantiate depending on ur UI (cards)

     public GameObject[] g_plantPrefab;
 

So let's say you have a tiles and you want to instantiate them on tiles..

Create a tile script and put this.

 using UnityEngine;
 using System.Collections;
 
 public class MyTile : MonoBehaviour {
 
     private GameObject m_slot;
     public Color g_startingColor;
 
     // Use this for initialization
     void Start ()
     {
 
         g_startingColor = GetComponent<SpriteRenderer>().material.color;
 
     
     }
     
 
     public bool IsSlotTaken()
     {
         return m_slot != null;
     }
 
     public void AssignToSlot ( GameObject a_tower)
     {
         if ( a_tower != null)
         {
             // our slot is now taken
             m_slot =a_tower;
 
             // Reparent the tower to us. Make it our child.
             m_slot.transform.parent = this.transform;
 
             // Rest the position
             Vector3 startpos = new Vector3 (0 , 0 ,-0.1f);
             m_slot.transform.localPosition = startpos;
         }
     }
 
     public void DestroySlot()
     {
         if ( m_slot)
         {
 
             Destroy ( m_slot);
             m_slot = null;
         }
     }
 }
 
 

Now to place the your card:

 using UnityEngine;
 using System.Collections;
 
 public class MyTilePlace : MonoBehaviour
 {
 
     private GameObject m_lastTile;
     public GameObject[] g_plantPrefab;
     public  int index;
 
     bool isCardSelected=  false;
     RaycastHit hitInfo;
     Ray ray;
 
 
     
     
     // Update is called once per frame
     void Update ()
     {
 
         Vector3 startpos = new Vector3 ( 0, 3 ,-0.149f);
 
 
         ray = GetComponent<Camera> ().ScreenPointToRay (Input.mousePosition);
 
         
         if (Input.GetMouseButton (0))
             {    
             if (m_lastTile != null) 
                 
                 { 
                     
                  if (m_lastTile.GetComponent<MyTile> ().IsSlotTaken () == false  )  
                     {
                         // instantiate your stuff on the tiles 
                         GameObject plants = (GameObject)Instantiate (g_plantPrefab [index] , startpos , Quaternion.Euler ( 90 , 0 ,0));
                         m_lastTile.GetComponent<MyTile> ().AssignToSlot (plants);
                         // the player cant keep spawning plants, he needs to select another card again.
                         isCardSelected = false;
                     }
 
                     }
                 }
         }
     }
 
     void SetLastTile (GameObject a_tile)
     {
         if (m_lastTile != a_tile) {
             // if we have a last tile, rest its color
             if (m_lastTile != null) {
                 // resrt its color
                 m_lastTile.GetComponent<SpriteRenderer> ().material.color = m_lastTile.GetComponent<MyTile> ().g_startingColor;
             }
 
             // copy over the tile
             m_lastTile = a_tile;
 
             // if we have a new valid tile , set its color 
             if (m_lastTile != null) {
                 m_lastTile.GetComponent<SpriteRenderer> ().material.color = Color.yellow;
             }
         }
     }
 
     public void onClickButton (int selectButton)
     {
         isCardSelected = true;
 
         index = selectButton;
 
     }
 
 }
 
 
 Noowww to your question ! :D!
 
 Attach MyTilePlace script to ny empty gameobject or at ur camera. and on your cards or UI Each UI should be a Button. After that attach the your gameobject that you attach MyTilePlace to ur button and use the function "onClickButton" This will ask you to put a integer. use the integer that you want to use for your card and array. For example in ur array you attach your normal gameobject it's index is 0. so on your UI u want to instantiate (create ) this normal thing put 0 index.
 
 Please vote if the answer help :D
 

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 VictorSIlvaK · Jul 17, 2016 at 11:00 PM 0
Share

Oh, thanks the answer, I will try it here .

avatar image

Answer by Unity_scat · Jul 17, 2016 at 07:17 PM

You always just use:

 var object : GameObject;
 
 function GetKeyDown(Input.GetKey(KeyCode.Space)) {
 Instantiate(object);
 }

You can also set the position of the instantiated object as well.

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 VictorSIlvaK · Jul 17, 2016 at 11:01 PM 0
Share

I had already tried it but nothing worked, or maybe I did something wrong. But even so thanks the time used to help.

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

216 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 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 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 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 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 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 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 avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

My UI Score Is Not Working? 1 Answer

How do i make it so when you are touched you are on the other teamlike in garrys mod hide and seek 1 Answer

Converting from Java 3 Answers

MobileSingleStickControl multitouch not supported. How fix? 0 Answers

UI Button Doesn't Render [C#] 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