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 E-H269 · Feb 22, 2015 at 04:28 PM · c#arrayaccesspublic variableother script

Problem with Accessing Variables from Other Scripts in c#.

I have an enemy ship and a player which is sopposed to shoot a projectile at the enemy and then the enemy should pull a value from an array from a script attaced to a controller.


     public GameObject gameController;
 
     void OnTriggerEnter (Collider other) {
         if (other.tag == "projectile1") {
             health -= gameObject.GetComponent<Controller>().damage[0];
         }
     }

And I have a controller scrtipt.


using UnityEngine; using System.Collections;

public class Controller : MonoBehaviour {

 public float[] damage;

 void Update () {
 
 }

}


I have tested this with hard coded values and it works just fine.

Just incase I somehow screwd up somewhere else in the script here is the full enemy script.(Some of the lines seem to be getting cut off in my preveiw so I there might be some brackets missing etc.)


 using UnityEngine;
 using System.Collections;
 
 public class BasicAI : MonoBehaviour {
 
     public GameObject gameController;
     public GameObject target;
     public float speed;
     public float stopDistance;
     public float attackDistance;
     public float weaponCoolDown;
     public Transform Barrel_L;
     public Transform Barrel_R;
     public GameObject projectile;
     public float health;
     public GameObject expolsionParticle;
     float weapomTimer;
 
     void Update () {
         transform.LookAt (target.transform);
 
         if (Vector3.Distance(target.transform.position, transform.position) > stopDistance) {
             rigidbody.AddRelativeForce (0, 0, speed);
         }
 
         else {
             rigidbody.AddRelativeForce (0, 0, -speed);
         }
 
         weapomTimer += 1 * Time.deltaTime;
 
         if (Vector3.Distance(target.transform.position, transform.position) < attackDistance) {
             Debug.Log("ready to fire");
             if (weaponCoolDown < weapomTimer) {
                 Instantiate (projectile, Barrel_L.position, Barrel_L.rotation);
                 Instantiate (projectile, Barrel_R.position, Barrel_R.rotation);
                 weapomTimer = 0;
                 Debug.Log("fire");
             }
         }
 
         if (health < 0) {
             Instantiate (expolsionParticle, transform.position, transform.rotation);
             Destroy (gameObject);
         }
     }
 
     void OnTriggerEnter (Collider other) {
         if (other.tag == "projectile1") {
             health -= gameObject.GetComponent<Controller>().damage[0];
         }
     }
 }



Comment
Add comment · Show 1
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 E-H269 · Feb 22, 2015 at 03:17 PM 0
Share

Sorry for that, as I said I tested the collision detection/damage system so that works, the problem seems to be this: health -= gameObject.GetComponent().damage[0]; particular line of code.

1 Reply

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

Answer by MakeCodeNow · Feb 22, 2015 at 04:55 PM

Arrays need to be allocated, not just declared. Somewhere you need to have damage = new float[X]; where X is the number of items you want to allow the array to store.

Comment
Add comment · Show 7 · 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 E-H269 · Feb 22, 2015 at 05:00 PM 0
Share

In the controller or enemy script?

avatar image E-H269 · Feb 22, 2015 at 05:12 PM 0
Share

So now my code looks like this:


 using UnityEngine;
 using System.Collections;
 
 public class Controller : $$anonymous$$onoBehaviour {
 
     public float[] damage;
 
     void Start () {
         damage = new float[10];
     }
 
     void Update () {
 
     }
 }



But sill for whatever reason it dosen't work.

avatar image shoffing · Feb 22, 2015 at 05:20 PM 0
Share

It looks like you're trying to access the 0'th element of an empty array. When you first instantiate (fancy word for using the "new" keyword) an array, all the elements are null - there's nothing there! Try putting something in the array at the 0th element and seeing if it still doesn't work:

 damage = new float[10];
 damage[0] = 30;

I'm not sure why you're using an array if you're only accessing the first element, but I'm sure you have your reasons.

avatar image Owen-Reynolds · Feb 22, 2015 at 05:32 PM 0
Share

Is damage supposed to be public (it is, just wondering if that was on purpose)? The size is whatever you type into the Inspector. If you're just going to remake it in code, no reason to be public.

But, the point of a public is usually you want to enter Inspector values.

I think this is a design issue. What's the purpose of the damage array at all?

avatar image E-H269 · Feb 22, 2015 at 05:34 PM 0
Share

The damage array is a chart of sorts like: projectile X dose X dmg.

Show more comments

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

4 People are following this question.

avatar image avatar image avatar image avatar image

Related Questions

Multiple Cars not working 1 Answer

Distribute terrain in zones 3 Answers

Global array list c# 1 Answer

How to access GameObject[] array in PlayMaker 1 Answer

How to check if a value exists in an array (C#) 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