• 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 /
This question was closed Jan 13, 2014 at 07:34 PM by Jamora for the following reason:

The question is answered, right answer was accepted

avatar image
0
Question by CKM92 · Jan 11, 2014 at 12:55 AM · raycastinstantiatemousegridcell

Instantiate Objects on Cells created during run time

Hey there, I created a grid:

 using UnityEngine;
 using System.Collections;
 
 public class Grid : MonoBehaviour {
 
     public Transform CellPrefab;
     public Vector3 Size;
     // Use this for initialization
     void Start () {
         CreateGrid ();
     }
     
     // Update is called once per frame
     void CreateGrid () {
         for (int x = 0; x < Size.x; x++) {
                         for (int z = 0; z < Size.z; z++) {
 
                                 Instantiate (CellPrefab, new Vector3 (x, 0, z), Quaternion.identity);
                         }
                 }
 
     }
 }

And I want to create cubes on top of the cells when i click them , therefor i used a raycasthit and the name of the hit object. The Problem i have is that im somehow not able to click on the cells. It works if i use a Plane and try to click on it but not on the cells.

 void Update () {
         if (Input.GetMouseButtonDown (0)) {
             RaycastHit hit;
             Ray ray = cam.ScreenPointToRay(Input.mousePosition);
             Debug.Log("1");
             if (Physics.Raycast(ray, out hit) && hit.transform.name=="Cell"){
                 //Instantiate (CannonPrefab);
                             Debug.Log("hit");
             }
                 }
     }

Im aware that the current script does not actual would spawn each cannon in each cell of the grid but i dont want to go on while i even cant click on the cells.

Does anyone have a clue why it wont work like this?

Comment
Add comment · Show 2
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 getyour411 · Jan 11, 2014 at 12:58 AM 1
Share

Are you spawned Cells actually named "Cell" or are they Cell(clone), Cell(clone), Cell(clone) since you aren't rena$$anonymous$$g your instantiated gameobject

avatar image robertbu · Jan 11, 2014 at 01:11 AM 0
Share

As @getyour411 indicates, they will have the name "Cell (clone)". You might want to use a tag ins$$anonymous$$d, or you can name them explicitly in your creation code. Plus if your cells are a mesh you've imported, you will have to add a collider of some sort to them.

1 Reply

  • Sort: 
avatar image
0

Answer by CKM92 · Jan 11, 2014 at 01:33 AM

omg^^

 void CreateGrid () {
         for (int x = 0; x < Size.x; x++) {
                         for (int z = 0; z < Size.z; z++) {
 
                             Object cl = Instantiate (CellPrefab, new Vector3 (x, 0, z), Quaternion.identity);
                 cl.name = "Cell";
 
                         }
                 }
 
     }

works now thx everybody

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

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

18 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

Related Questions

how to tell if fence encloses area 1 Answer

raycast and instantiat advice 0 Answers

Instantiating Object On Map 1 Answer

How to prevent GameObject from spawning on top of each other? 1 Answer

Grid Placement In Game 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