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 Romano185 · Feb 21, 2012 at 06:17 PM · playerenemygetcomponentdamagestatic-variables

Problem with getcomponent replacing static variables

Hi everybody

I've been working on replacing the static variables in the script I use for my enemies, because I want to add more then 1 enemy. So I've replaced the static variable used for the enemies health by a normal variable I reach using FindWithTag(All my enemies have the tag Enemy) and GetComponent. But there's a problem in my script. The enemy immediately dies when the player is instantiated

These are my scripts:

The script attached to the enemy prefab, EnemyProximity:

     static var aggroDistance = 10;
     var mytransform : Transform;
     var otherobject : GameObject;
     var othertransform : Transform;
     var glitchCorrector : boolean = true;
     var tickerInterval : boolean = false;
     var tickerRate = 5.0;
     private var nextTick = 0.0;
     var drop : GameObject;
     var enemyHealth : int;
     static var seppuku : boolean = false;
     var attackscript : AttackScript;
     
     function Awake (){
     otherobject = null;
     othertransform = null;
     enemyHealth = 100;
     }
     
     function Update () {
     
     if (ClassSelectionScript.classselected == true && otherobject == null && othertransform == null ){
     otherobject = GameObject.FindWithTag("Player");
     othertransform = otherobject.GetComponent(Transform);
     attackscript = otherobject.GetComponent(AttackScript);
     }
     
 if (attackscript != null && otherobject != null && attackscript.enemyHealth <=     enemyHealth){
   enemyHealth = attackscript.enemyHealth;
    }
     
     if (othertransform != null && Vector3.Distance(mytransform.position, othertransform.position) < aggroDistance && glitchCorrector == true){
     ArcherHealthScript.curhealth += 10;
     WarriorHealthScript.curhealth += 10;
     MageHealthScript.curhealth += 10;
     glitchCorrector = false;
     }
     
     if (othertransform != null && Vector3.Distance(mytransform.position, othertransform.position) > aggroDistance && glitchCorrector == false){
     glitchCorrector = true;
     }
     
     if(othertransform != null && Vector3.Distance(mytransform.position, othertransform.position) < aggroDistance && tickerInterval == false && HealthScript.curhealth > 0){
     ArcherHealthScript.curhealth -= 10;
     MageHealthScript.curhealth -= 10;
     WarriorHealthScript.curhealth -= 10;
     tickerInterval = true;
     }
     
     if(tickerInterval == true && Time.time > nextTick){
     nextTick = Time.time + tickerRate;
     tickerInterval = false;
     }
     
     if(enemyHealth <= 0){
     seppuku = true;
     Destroy(gameObject);
     var instance : GameObject = Instantiate(drop, transform.position+Vector3(0,0.2,0), transform.rotation);
     ExperienceScript.curexp += 10;
     }
     }

And the script attached to the player, called AttackScript:

 static var distance = 10;
 var myobject : GameObject;
 var otherobject : GameObject;
 var mytransform : Transform;
 var othertransform : Transform;
 var cooldown : boolean = false;
 var activetexture : GUIStyle;
 var inactivetexture : GUIStyle;
 var usedtexture : GUIStyle;
 var enemyproximity : EnemyProximity;
 var enemyHealth : int;
 static var interval : float;
 
 
 function Start (){
 
 usedtexture = activetexture; 
 myobject = GameObject.FindWithTag("Player");
 otherobject = GameObject.FindWithTag("Enemy");
 mytransform = myobject.GetComponent(Transform);
 othertransform = otherobject.GetComponent(Transform);
 enemyproximity = otherobject.GetComponent(EnemyProximity);
 enemyHealth = enemyproximity.enemyHealth;
 interval = 10;
 
 }
 
 function OnGUI (){
 
 if (GUI.Button (Rect(0,500,100,100),GUIContent("Attack"),usedtexture) && Vector3.Distance(mytransform.position, othertransform.position) < distance && enemyHealth > 0 && cooldown == false){
 enemyHealth -= 50;
 cooldown = true;
 Timer();
 }
 }
 
 function Update (){
 if (enemyproximity.enemyHealth > enemyHealth){
 enemyHealth = enemyproximity.enemyHealth;
 }
 }
 
 function Timer (){
 
 if(cooldown == true){
 usedtexture = inactivetexture;
 yield WaitForSeconds(interval);
 cooldown = false;
 usedtexture = activetexture;
 }
 }

The problem is caused by .GetComponent I think. And I also get a error in line 27 of the EnemyProximity script, saying object reference not set to an instance of an object.

Please help me solve these problems!

Comment
Add comment · Show 6
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 Romano185 · Feb 24, 2012 at 10:25 AM 0
Share

Unity says the problem is in the if statement, maybe I've made a mistake with < and <=?

avatar image Romano185 · Feb 26, 2012 at 01:03 PM 0
Share

What does the error Object reference not set to an instance of an object mean?

avatar image syclamoth · Feb 26, 2012 at 02:10 PM 0
Share

It means the thing you are trying to reference doesn't exist.

avatar image Romano185 · Feb 26, 2012 at 03:51 PM 0
Share

But all the things I reference exist, there is a variable in attackscript called enemyHealth, and the variables otherobject and attackscript do exist. How is it possible Unity thinks those things do not exist?

avatar image aldonaletto · Feb 26, 2012 at 04:08 PM 0
Share

$$anonymous$$ore specifically, Unity is telling you that the variable that should reference the object is null. If line 27 is what I suppose, you should revert the expressions order to avoid this problem:

if (attackscript != null && otherobject != null && attackscript.enemyHealth 
The code generated here verifies attackscript first; if it's not null, otherobject is verified; finally, if both are non null, attackscript.enemyHealth is compared. This is a smart feature created in C and used in all modern languages: conditional expressions are evaluated from left to right, and only if necessary - in this case, the first false result aborts the whole if, thus the other expressions are not evaluated.
Show more comments

0 Replies

· Add your reply
  • Sort: 

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

7 People are following this question.

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

Related Questions

Player Damage to enemy damage melee system 0 Answers

Dungeon Enemy Help 1 Answer

Player attacks once but the enemy takes double damages! 1 Answer

Question on Player Damage 1 Answer

auto select enemys 3 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