• 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 post has been wikified, any user with enough reputation can edit it.
avatar image
0
Question by r0b3rt97 · Oct 09, 2013 at 10:44 PM · tagforcename

problem with raycasts and tags

i've created a few cubes to test my shot force..i mean when i wanted when i shot at some object that object move forward by force here's the scripts

var range = 500; var shotForce = 100;

function Update() { var bullet : RaycastHit; if(Physics.Raycast(transform.position, transform.forward, bullet, range)) { if(bullet.transform.tag == "Map") { gameObject.FindWithTag("Map").rigidbody.AddForceAtPosition(transform.forward * shotForce, bullet.point); } } Destroy(gameObject); }

So when i shot object "cube1" with tag "Map" it will move forward by force but when i shot object "cube2" also with tag "Map" , the object "cube1" moves instead of object "cube2".

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 meat5000 ♦ · Oct 09, 2013 at 10:46 PM 0
Share

You dont need to findwithtag. You have the object captured in the if statement.

 bullet.gameObject.rigidbody.AddForceAtPosition(transform.forward * shotForce, bullet.point);
avatar image r0b3rt97 · Oct 09, 2013 at 10:55 PM 0
Share

then i got this error

$$anonymous$$issingFieldException: UnityEngine.RaycastHit.gameObject Boo.Lang.Runtime.DynamicDispatching.PropertyDispatcherFactory.FindExtension(IEnumerable' 1 candidates)

1 Reply

· Add your reply
  • Sort: 
avatar image
0

Answer by VesuvianPrime · Oct 09, 2013 at 11:44 PM

You can get the hit collider by looking at the bullet.collider

http://docs.unity3d.com/Documentation/ScriptReference/RaycastHit-collider.html

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.

Welcome to Unity Answers

The best place to ask and answer questions about development with Unity.

To help users navigate the site we have posted a site navigation guide.

If you are a new user to Unity Answers, check out our FAQ for more information.

Make sure to check out our Knowledge Base for commonly asked Unity questions.

If you are a moderator, see our Moderator Guidelines page.

We are making improvements to UA, see the list of changes.



Follow this Question

Answers Answers and Comments

17 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

Related Questions

RPC isn't called on objects with another name, even though they have the same script. 1 Answer

Multiplayer Chat Script (Problem : mixed names) 1 Answer

Duplicate object and keeping original name 2 Answers

Zombie Round Script Help 1 Answer

Update Script 1 Answer

  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges