Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 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 shepsaus000 · Oct 06, 2015 at 06:43 PM · scripting problem

How to prevent a spawn if my ray is on a certain object?

Here is my code: using UnityEngine; using System.Collections;

 public class Instantiate : MonoBehaviour {
     //position and rotation can be equal to that of an object if you use the object's ID.
     Ray ray;
     RaycastHit hit;
     public GameObject Sphere;
 
     void Update ()
     {
             ray = Camera.main.ScreenPointToRay (Input.mousePosition);
         if (Physics.Raycast (ray, out hit))
         {
             if (Input.GetMouseButtonDown (0))
             {
                 Instantiate (Sphere, hit.point + new Vector3(0, 0.5f, 0), Quaternion.identity);
 
             }
         }
     }
 }

How can I make it so that when the ray hits a sphere clone, the instantiation code does not execute? Keep in mind that I am new to Unity, so try to use simple terms on me.

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

1 Reply

· Add your reply
  • Sort: 
avatar image
0

Answer by Mr_Edward · Oct 06, 2015 at 06:59 PM

It's actually really simple: All you have to do is to test if your raycast is hitting the sphere clone. We can do this simply by checking if the name of the object we've hit is "Sphere (clone):

 public class Instantiate : MonoBehaviour {
     //position and rotation can be equal to that of an object if you use the object's ID.
     Ray ray;
     RaycastHit hit;
     public GameObject Sphere;
     
     void Update ()
     {
         ray = Camera.main.ScreenPointToRay (Input.mousePosition);
         if (Physics.Raycast (ray, out hit))
         {
             if (Input.GetMouseButtonDown (0))
             {
                 if(hit.transform.name == "Sphere (Clone)")
                 {
                     Instantiate (Sphere, hit.point + new Vector3(0, 0.5f, 0), Quaternion.identity);
                 }
             }
         }
     }
 }
Comment
Add comment · 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 shepsaus000 · Oct 07, 2015 at 03:15 PM 0
Share

Although this would be good for when I want something to clone at a sphere, I actually do not want it to clone at all. I just want to say this basically

If: I click at a sphere or one of its many clones.

Then: No sphere is made.

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

4 People are following this question.

avatar image avatar image avatar image avatar image

Related Questions

How can one rotate an object instantly? 2 Answers

Card Game like yu-gi-oh 1 Answer

First Person Controller - Problems with footstep audio being played too fast. 0 Answers

Iterate through area descibed by objects 1 Answer

MonoType Behavior 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