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 TacoMakerMan · Jul 31, 2017 at 02:45 AM · variablesmonobehaviourpublic variable

Public variable containing script

I have a script that shoots a laser and when it hits a certain object it gets a script and sets a bool to true, so that script knows it's being hit by the laser.

However I am using the laser in multiple stages and so each object should react differently and therefore have different scripts, so I need something like this:

 Public Script objectScript;
 Public GameObject object;
 
 void Update ()
 {
     object.GetComponent<objectScript> ().LaserHitting = true;
 }
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
0

Answer by Commoble · Jul 31, 2017 at 03:08 AM

Firstly, let me make sure I understand what you're asking: You have a laser object, and several other objects, and you want each of these other objects to react differently when the laser hits them?

Of the many ways to do this, one way would be to use the OnCollisionEnter message for the objects being hit, and check if the other object that collided with it is a laser object (for example, by calling GetComponent<Laser> != null, or checking the collider's physics layer ID if lasers have a physics layer all to themselves).

So it would be something like

 public class Squirrel : MonoBehaviour
 {
     void OnCollisionEnter(Collision collision)
     {
         // if the object that collided with this has a Laser component
         if (collision.gameObject.GetComponent<Laser> != null)
         {
             this.doWhatTheSquirrelDoesWhenHitByALaser();
         }
     }
 }
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 TacoMakerMan · Jul 31, 2017 at 05:22 AM 0
Share

This wouldn't work, the laser is not a physical object, it's just a lineRenderer.

avatar image
0

Answer by Bunny83 · Jul 31, 2017 at 03:27 AM

There are several ways to accomplish that. First of all changing a public variable on another script is bad design. Strict OOP doesn't know something like public fields / variable. Interactions with other classes should always be done through methods.

First approach is using an interface. You can define an interface with some specific methods which every class that implements that interface has to provide. Your laser can simple search for that interface and GetComponent will return the first class that implements that interface:

 public interface ILaserTargetable
 {
     void HitByLaser();
 }
 
 public class SomeScript : MonoBehaviour, ILaserTargetable
 {
     // ...
     public void HitByLaser()
     {
         // do something
     }
 }
 
 // in your laser class you can do:
 
 ILaserTargetable target = hitObject.GetComponent<ILaserTargetable>();
 if (target != null)
 {
     // we found a class that implements "ILaserTargetable", so call HitByLaser:
     target.HitByLaser();
 }

Another solution is to use Unity's SendMessage. This is the most easiest way. In your laser script you can simply call SendMessage on the target gameobject and specify the name of method that should be called as string. Now the script on the object that you hit just need to implement that method:

 public class SomeScript : MonoBehaviour
 {
     public void HitByLaser()
     {
         // do something
     }
 }


 // in your laser class you can do:
 
 hitObject.SendMessage("HitByLaser");

Note that SendMessage will throw an error if there is no script on the hitObject that has a method with that name. However you can pass "SendMessageOptions.DontRequireReceiver" as additional parameter. This will just send off the message and if there's a script with a method with the specified name, that method will be called, if not nothing will happen.

SendMessage has a slight overhead. So it's not recommended to use it every frame. However for most hit event propergation there's no problem.

Instead of an interface one could also use a baseclass from which all your other script are derived from. However since Unity encourages object composition through components instead of class inheritance, this is not very flexible and i wouldn't recommend it. Inheritance only makes sense when large amount of those scripts are basically the same and just small parts are different.

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

69 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 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 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

Public variable not showing in inspector using new Input System 2 Answers

Trying to get access to variables in the editor script 4 Answers

Variable declaration. Public vs indirect public? 3 Answers

Editing external vars from another script problem? 1 Answer

Question about creating variables to hold scripts. 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