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 /
This post has been wikified, any user with enough reputation can edit it.
avatar image
0
Question by harleylaurie · Jul 24, 2012 at 09:55 PM · javascriptvariablenullreferenceexceptionstaticglobal

Is there any way to edit variables inside other scripts without declaring them as static?

I am aware that questions similar to this may have been asked many times already but I believe my situation may be unique.
In my current game I have some basic enemies, their health is displayed via the use of a plane that is floating above their heads, facing the camera. I have different textures which I change around at different health levels to indicate how much health the enemy has left.
The player can press tab to target different enemies, and left click to attack the current target, which accesses the enemies health variable and subtracts 1 from its value.
The problem I am having, is that when I attack one enemy, health is removed from every enemy in the scene, no matter how far away they are.
I already made a post on the unity3d subreddit and they told me that the problem was that I have the variable set as static.
However, if I try setting this to any other type (I have tried all that I know of, static, public, normal) I receive a NullReferenceException error when the player try's to attack an enemy.

Following is the code for the script which controls the players targeting and attacking.

private var target : GameObject;
private var attackDistance = 3;
var targetIndicator : GameObject;
var targetInfo : GameObject;

function Update()
{
if (Input.GetKeyDown ("tab") && (FindClosestEnemy() != null))
{
target = FindClosestEnemy();
var oldIndicator = GameObject.FindWithTag ("targetIndicator");
Destroy (oldIndicator);
var targetHealth = target.GetComponent(EnemyHealth);
Instantiate (targetIndicator, target.transform.position, target.transform.rotation);
}
if (target != null)
{
if (Input.GetButtonDown ("Fire1") && (Vector3.Distance(target.transform.position, transform.position) < attackDistance))
{
targetHealth.health -= 1;
}
}
}

function FindClosestEnemy () : GameObject
{
// Find all game objects with tag Enemy
var gos : GameObject[];
gos = GameObject.FindGameObjectsWithTag("Enemy");
var closest : GameObject;
var distance = Mathf.Infinity;
var position = transform.position;
// Iterate through them and find the closest one
for (var go : GameObject in gos) {
var diff = (go.transform.position - position);
var curDistance = diff.sqrMagnitude;
if (curDistance < distance) {
closest = go;
distance = curDistance;
}
}
return closest;
}

function clearTarget ()
{
target = null;
}

Following this, is the script which controls the enemies health, and updates the health bar accordingly.

var healthBar : GameObject;
var healthFull : Texture;
var health5 : Texture;
var health4 : Texture;
var health3 : Texture;
var health2 : Texture;
var health1 : Texture;
static var health : int = 6;

function Update ()
{
if(health > 6)
{
health = 6;
}

switch(health) { case 6: healthBar.renderer.material.mainTexture = healthFull; break;

case 5: healthBar.renderer.material.mainTexture = health5; break;

case 4: healthBar.renderer.material.mainTexture = health4; break;

case 3: healthBar.renderer.material.mainTexture = health3; break;

case 2: healthBar.renderer.material.mainTexture = health2; break;

case 1: healthBar.renderer.material.mainTexture = health1; break;

case 0: var oldIndicator = GameObject.FindWithTag ("targetIndicator"); Destroy (oldIndicator); Destroy(gameObject); break; } }

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
0

Answer by Seth-Bergman · Jul 24, 2012 at 10:03 PM

the problem is this line :

var targetHealth = target.GetComponent(EnemyHealth);

you are declaring the var targetHealth INSIDE the if statement...

just move the declaration outside of the if statement:

 function Update(){
 var targetHealth : EnemyHealth;
 if (Input.GetKeyDown ("tab") && (FindClosestEnemy() != null))
 {
 target = FindClosestEnemy();
 var oldIndicator = GameObject.FindWithTag ("targetIndicator");
 Destroy (oldIndicator);
 targetHealth = target.GetComponent(EnemyHealth);
 
 //...etc
 }

because of the scope, if it is declared inside the if statement, it only exists inside the statement..

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

Global variables - static keyword ? UnityScript? javascript? 1 Answer

Can't access a javascript static variable from c# script 1 Answer

Static Variable Problem 1 Answer

Should I create local copy of global variable? 3 Answers

Static function and variables error 2 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