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 /
  • Help Room /
avatar image
0
Question by Jacob_Anthony · Feb 03, 2017 at 07:15 PM · gameobjectvariables

How to get access to variable from another script without knowing script's name c#

hey i wonna do a detection and if i detect gameobject i want to get access to variable without knowing script's name

Comment
Add comment · Show 4
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 tanoshimi · Feb 03, 2017 at 07:16 PM 0
Share

What would be the point? If you don't know what type of class it is, you can't know what methods or variables it contains so you couldn't do anything useful with it anyway.

avatar image Jacob_Anthony tanoshimi · Feb 03, 2017 at 07:28 PM 0
Share

it would be something like dealing damage. If i know by detection gameobject i want modify its points. Its only 2 prefabs and detection also brings info about type of gameobject so i know which of these 2 is gameobject

avatar image JaredHD Jacob_Anthony · Feb 03, 2017 at 08:21 PM 0
Share

Well if it's damage then you can add a trigger box collider to a sword/bullet. Then check if the sword/bullet hit an enemy and if so retrieve a script on the enemy that handles his health and apply some damage to the enemy. You'd need to use the `.GetComponent()` to retrieve the script. Then after the brackets find a public variable and edit that, or better use a method designed for applying damage to the enemy.

To detect if you hit the enemy use the `OnTriggerEnter(Collider other)` method with the sword/bullet.

avatar image pandatree12 · Feb 03, 2017 at 08:26 PM 0
Share

What are you trying to do? Take damage/deal damage? A point system? I can help you I think but I need to know what you are using it for.

2 Replies

· Add your reply
  • Sort: 
avatar image
1

Answer by Bonfire-Boy · Feb 04, 2017 at 02:32 PM

It just doesn't make sense to say that you literally want ANY GameObject to be capable of taking damage.

If you want to make it so that you can MAKE any GameObject capable of taking damage, then create a script to implement this functionality, and add it to any GameObject that you want to have the functionality. This is what the whole business of creating MonoBehaviour classes and adding them to GameObjects as components is about - making objects capable of exhibiting specific kinds of behaviour.

The script could be very generic, something as simple as this would work....

 class DamageTaker : MonoBehaviour
 {
 public float damage;
 }

When a collision is detected between something that can inflict damage and another GameObject, you would check that second GameObject for a DamageTaker component, and if you found one you could then modify its damage value.

This is deliberately simplistic because you seem to be demonstrating a lack of this fundamental idea of creating and adding behaviour to GameObjects. But a simple class like this does have the benefit of being totally flexible, which seems to be what you're after.

What you do with the damage is another matter, and would be implemented in other classes. You could for example create a Player class and add a component of that type to same GameObject as the DamageTaker. This could for example check its Damage component's damage value in its Update function and do something with it (probably "consuming" the damage by then resetting it to zero). But you could create other classes which used the damage value in different ways.

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
avatar image
0

Answer by pandatree12 · Feb 04, 2017 at 05:14 PM

I'm not sure if that meant you figured it out? But if not here's the easiest way:

 #pragma strict
 
 //THIS SCRIPT WOULD BE FOR THE THING ATTACKING THE PERSON
 var damage = 100f;
 
 function OnCollisionEnter (hit : Collision) {
 
     if (hit.gameObject.tag == "Player") {
 
         hit.transform.SendMessage("DealDamage", damage, SendMessageOptions.DontRequireReceiver);
         //That code tells the player to do the function DealDamage and it transfer the damage variable over, but not permanatly
 
     }
 
 }
 
 #pragma strict
 
 //THIS SCRIPT WOULD BE FOR THE PLAYER
 var health == 100f;
 
 function DealDamage(damage : float) {
 
     health -= damage; //damage variable is transfered to this line of code
 
 }



It is kind of difficult to permanently transfer a variable, so most of the time it is better to combine scripts into one, but when dealing with player health/damage, or points then use something like this.

Sorry it's in Java, everything should work as long as you change the variables to whatever they use in C# like public float damage = 100; and I think you have to add something into the front of the function like void? I don't know but you know so good luck.

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

6 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Novice Question: Assigning GameObject properties efficiently. 1 Answer

storing velocity and temporarly using it 0 Answers

Access another object's variable 1 Answer

Game Objects not using same script independently. 1 Answer

Assigning a GameObject variable to equal another GameObject variable via C# script. 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