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 Jokeaccount · Apr 09, 2013 at 12:22 AM · c#arraysnull reference

Creating an array of structs

Hi,

We want to create a priority system for some of our game objects to act in a battle scene and we encountered the following problem:

Our gameobjects have a script called stats and this script calls a GetPriorities function on object spawn. This GetPriorities function exists in another script called TurnSystem which is an empty object's component that is supposed to manage the turns of action of the objects.

The Stats script is:

public class Stats : MonoBehaviour { public int Level; public int HP; public int Damage; public TurnSystem system; public int BattlePriority; // Use this for initialization void Start () {

 }
 
 // Update is called once per frame
 void Update () {
 
 }
 
 void OnSpawned(){
     BattlePriority=system.GetPriority(Level,this.gameObject); <--line 21
 }

}

The turnsystem script is:

using UnityEngine; using System.Collections;

public class TurnSystem : MonoBehaviour {

 public class Turns {
     private int priority;
     private GameObject go;
     
     public int Priority{
          get{
              return priority;
          }
          set{
              priority=value;
          }
      }
      public GameObject Go{
          get{
              return go;
          }
          set{
              go=value;
          }
      }
             
 }
 
 public int i; //Array ordering int, needs reseting after each battle
 public Turns[] BattleTurns=new Turns[10];
 
 // Use this for initialization
 void Start () {
 
 }
 
 // Update is called once per frame
 void Update () {
 
 }
 
 public int GetPriority(int a,GameObject go){ 
     int priority;
     priority=a*100+a*100*((Random.Range(-20,20))/100); //Priority Setting function, needs testing
     SetPriority(priority,go);
     return priority;
 
 }
 
 public void SetPriority(int b,GameObject went){        
     BattleTurns[i].Priority=b;  <--line 51
     BattleTurns[i].Go=went;
     i++;
 
 }

}

When we try to run it, Unity gives the following error:

NullReferenceException: Object reference not set to an instance of an object TurnSystem.SetPriority (Int32 b, UnityEngine.GameObject went) (at Assets/Scripts/TurnSystem.cs:51) TurnSystem.GetPriority (Int32 a, UnityEngine.GameObject go) (at Assets/Scripts/TurnSystem.cs:45) Stats.OnSpawned () (at Assets/Scripts/Stats.cs:21)

I'm guessing something is wrong in the way we declare stuff on GetPriority but I can't find how to properly fix it...

Thanks in advance

Comment
Add comment · Show 12
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 Loius · Apr 09, 2013 at 12:47 AM 1
Share

"asked 20 $$anonymous$$utes ago by Jokeaccount"... hm. this leads me to:

"public int i;" WHAT?! AHAHAHAHA AHAHAHAHA AHAHAHAHAHAHAHAHHHAHAHA thump

on the off chance that this is serious:

you need to set BattleTurns[i] = new Turns() before trying to mess with it.

avatar image Orfen · Apr 09, 2013 at 02:49 AM 0
Share

Loius, your answer gives back "cannot implicitly convert TurnSystem.Turns to TurnSystem.Turns[]"

$$anonymous$$ight wanna check if the code you suggest actually works before being all funny and stuff.

avatar image Chronos-L · Apr 09, 2013 at 02:59 AM 3
Share

@Orfen, did you type BattleTurns[i] = new Turns() or BattleTurns = new Turns(). The first is the answer provided by @Loius , and the second will produce a "Cannot implicitly convert Turns to Turns[]"

avatar image Loius · Apr 09, 2013 at 03:48 AM 0
Share

aw, lighten up. :D

"joke account" posted a class with a publicly visible member called 'i'. nerds should find that worth a chuckle at the very least :)

but it is a Turns and not a Turn, which is what i wrote. i'll give you that one. :)

avatar image whydoidoit · Apr 09, 2013 at 04:13 AM 2
Share

Think I might create an account called pleasedontanswermyquestion

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

15 People are following this question.

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

Related Questions

Multiple Cars not working 1 Answer

Distribute terrain in zones 3 Answers

Null reference when accessing GameObject in the Array(C#) 1 Answer

Vector3 resultant array sorting 2 Answers

creating and manipulating a grid efficiently 1 Answer


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