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 CiberX15 · Aug 02, 2013 at 06:59 AM · gameobjectreferencebulletinstance

How to check against the object type of an instance

Ok so I have a bullet that is fired out of my space ship and then replaces itself with an explosion when it hits something (2D game so when it passes over an object). The problem is it is also exploding when it hits other bullets and explosions which would be cool option but not what I am looking for right now. So I tried to set it up in my script here:

 #pragma strict
 
 var ProjectileSpeed : float = 10;
 var ProjectileLife    : float = 5;
 var ImpactType : GameObject;
 var owner : GameObject;
 var canHitSelf = false;
 var canHitSplode = false;
 
 
 
 function Start () 
 {
     rigidbody.velocity += transform.forward * (ProjectileSpeed * 10 * GetComponent(Rigidbody).mass);
     
     var locVel = transform.InverseTransformDirection(rigidbody.velocity);
     locVel.x = 0;
     rigidbody.velocity = transform.TransformDirection(locVel);
 }
 
 
 
 function Update () 
 {
     Destroy(gameObject, ProjectileLife);
     
     var HitObject : GameObject;
     
     //create ray from mouse and... yeah actualy I dont know how this works.
     var ray = new Ray (transform.position, Vector3.down);
     
     //Used to store what we hit
     var hitInfo : RaycastHit;
     
     //use ray to find object
     //Physics.Raycast(ray.origin, ray.direction, hitInfo);
     
     if(Physics.Raycast(ray.origin, ray.direction, hitInfo))
     {
         //get the colliders parent which should be our hit object
         HitObject = hitInfo.collider.gameObject;
         
         if(HitObject != owner)
         {
             if(canHitSelf == false && HitObject != gameObject || canHitSelf == true)
             {
                 if(canHitSplode == false && HitObject != ImpactType || canHitSplode == true)
                 {
                     //Create explosion
                     Instantiate(ImpactType, transform.position, transform.rotation);
                     
                     //Get rid of bullet
                     Destroy(gameObject);
                 }
             }
         }
     }
 }

This had no effect. What I think is happening is that the if statement is looking for the specific instance specified rather than the type of object (e.g. bullet and impact), however, I don't know how I would reference the type rather than the instance. I would also rather not have to add more variables if I could help it, for example I am lookig for something along the lines of a "HitObject != ImpactType.ObjectType" but if that simply is not possible I understand.

Thanks in advance for the help.

Comment
Add comment · Show 1
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 chronicfail · Aug 02, 2013 at 07:09 AM 0
Share

You could just set the bullets and explosions to the Ignore Raycast layer, and then they will be ignored by all raycasting and rays will just pass through them.

It's not ideal as you won't be able to shoot at bullets at all (unless using collisions).

1 Reply

· Add your reply
  • Sort: 
avatar image
0
Best Answer

Answer by Jamora · Aug 02, 2013 at 07:42 AM

Your if clauses are quite peculiar:

   if( canHitSelf == false && HitObject != gameObject || canHitSelf == true)
           true/false              true/true                   false/true

What I'm getting at is that this particular if will always return true.

As for your original problem. I think you should have a script, say ExplodableObjectScript in each object that you want your bullets to explode on. Then, you could check

 if (HitObject.GetComponent< ExplodableObjectScript >() != null)
 //explode

Although depending on your project's needs, a simple tag could also suffice. In which case you could check

     if (HitObject.tag.Equals(ExplodableObject))
     //explode
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 CiberX15 · Aug 03, 2013 at 02:14 AM 0
Share

Yeah I realized after I posted that that first if statement was broken. But again when I wrote the code I was thinking it would look at gameObject to mean the type of gameObject not the specific instance of the game object.

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

16 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

Related Questions

Why this Texture gives value of Null when I access it from other script 1 Answer

Referencing instance of game object hit via RaycastHit 1 Answer

How do I assign a prefab reference to a private variable? 1 Answer

Passing Object References Through Function 0 Answers

Null Reference at (wrapper managed-to-native) 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