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 doomprodigy · Jan 16, 2012 at 06:00 AM · gameobjectboolean

Accesing another Script on gameObject and asking if it is true.

Alright so Im a bit stuck. What I am trying to do is access a script on the gameObject which is where I handle damaged (I prefer to keep a few things out from my AI script such as damage) and what I am trying to do it that when I shoot my enemy when I am out of his field of view he changes his state to attacking me.

Now this is simple enough you can just put an if statement wondering if that scripts static boolean values gettingHurt is true. This works fine but if I shoot an enemy with the damage receiving script since that scripts become true the AI script reads through all damage receiving scripts and since one gettingHurt is true it changes its state.

 if (DamageReceiver.gettingHurt == true){
     transform.LookAt(target);
         if (ableToShoot == true)
             {
                 InvokeRepeating("Shoot", 0.001f, gunROF);
                 ableToShoot = false;
             }
     }

(This is a seperate AI code for a MG nest which is not using a state machine but it still needs the same thing done to it. Figured I might show some script.)

Also I have tried using gameObject.GetComponent in various methods but I can not seem to get it to work right or I get the error (something forgot the first part as I have reverted script so it works so I could work and test other things )cannot be accessed with an instance reference, qualify it with a type name instead

Thank you for your help :)

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

1 Reply

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

Answer by Lo0NuhtiK · Jan 16, 2012 at 07:01 AM

You said above that your gettingHurt variable is a static variable, so (from what I've gathered) changing that variables value anywhere will change it's value everywhere .
If you want each AI to have it's own instance of that variable, then it needs to be non-static.

quick example of how to access the AI script from a projectile->

 var health_AI : AI_HealthScriptName ;
 function OnCollisionEnter(other : Collision){
    //get the script from the AI this projectile just hit
    health_AI = other.collider.gameObject.GetComponent(AI_HealthScriptName) as AI_HealthScriptName ;
    //set the hurtvariable
    health_AI.gettingHurt = true ;
    //extra example assuming we're going to take some health away somehow
    health_AI.currentHealth -= 50 ;
    //reset the health_AI to nothing/null since we're done with it
    health_AI = null ;
 }

Something along those lines.

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 doomprodigy · Jan 16, 2012 at 07:26 AM 0
Share

/facepalm. No wonder. Ill have a play and see if I can work it out.

avatar image doomprodigy · Jan 16, 2012 at 07:43 AM 0
Share

Success I managed to erase that static part in my script and it worked straight. It is always them easy things that get me :/

Cheers $$anonymous$$ate,

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

5 People are following this question.

avatar image avatar image avatar image avatar image avatar image

Related Questions

Unity crashes after I destroy a gameobject 1 Answer

How to recycle random prefabs that spawn from a specific point. 1 Answer

Cannot Locate Boolean in different script 4 Answers

Collecting Array items in order 1 Answer

check if object is moving 5 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