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
1
Question by Voridian · Jul 19, 2014 at 01:15 PM · c#raycastfloat

How can i change a variable on the object hit by a raycast C#

im sure theres a simple solution to this but i dont know it and i dont really know how to phrase the question. i have a simple raycast script here which works fine but i want to know how i can change a float variable on what i hit's script:

     void Update () {
         RaycastHit hit;
         Ray shooterRay = new Ray(transform.position, transform.forward);
         if(Input.GetButton("Fire1")){
             Debug.DrawRay (transform.position, transform.forward, Color.green);
                 if(Physics.Raycast(shooterRay, out hit, Mathf.Infinity)){
                 if(hit.collider.tag == "Enemy"){
                     Debug.Log("Hit");
                     hit.EnemyCC.health -= 15;
                     // EnemyCC is the name of the script i want to change, health is the float
                 }
             }
             }
         }
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

2 Replies

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

Answer by robertbu · Jul 19, 2014 at 01:39 PM

   hit.transform.GetComponent<EnemyCC>().health -= 15;

http://docs.unity3d.com/412/Documentation/ScriptReference/index.Accessing_Other_Game_Objects.html

http://unitygems.com/script-interaction1/

Comment
Add comment · Show 2 · 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 Voridian · Jul 19, 2014 at 02:14 PM 0
Share

AH thanks so much, i was typing other variations on that but i couldnt get it to work, i feel kinda silly now.

avatar image robertbu · Jul 19, 2014 at 02:27 PM 1
Share

Various forms of this problem come up on UA every day, so don't feel silly. The thing to remember is that a script can go on multiple objects, so, unless you've declared variables as static, you need to get the specific instance of that script (component) to access variables or functions.

avatar image
1

Answer by sbroscious · Jul 19, 2014 at 02:35 PM

You can also do something like this:

 float informationYouWantSent;
 
 if(Physics.Raycast(shooterRay, out hit, Mathf.Infinity)){
            hitInfo.transform.SendMessage ("nameOfFunctionOnTargetHitInStringForm", informationYouWantSent, SendMessageOptions.DontRequireReceiver);//this sends the informationYouWantSent to the nameOfFunctionOnTargetHit() if there is no nameOfFunctionOnTargetHit() this code sends no information.
            //all the other stuff the Ray does here
 }

in your EnemyCC class you would have something like:

     float informationExpected;
     float informationYouWantToChange;
 
      void nameOfFunctionOnTargetHit(float informationExpected){
          //do something to informationYouWantToChange with informationExpected
     }


With this method you never have to worry about if the enemy is properly tagged or not, you just need to ensure the gameObject has the nameOfFunctionOnTargetHit() in one of its components if you actually want anything done to the gameObject. You can also send multiple "messages" to multiple gameObjects with the same raycast.

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.

Follow this Question

Answers Answers and Comments

23 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

Related Questions

How can i modify a float in another script in C#? 1 Answer

cant kill more than one enemy C# 2 Answers

AI Shooting for Aircraft 1 Answer

Something wrong with the raycast 1 Answer

Float not working all the time? 1 Answer


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