1

Alright, so im not sure exactly whats going on, i have been searching around other posts with the same/similar titles, but none of them are clear on whats wrong with what im doing, i have a public GameObject that i set to another object, im trying to add a variable into an array from the variables GameObjects component (script) but it keeps giving me this error, here is my code.

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class Dice : MonoBehaviour
{

    public int diceNumber = 0;
    public bool diceActive = true;
    public GameObject rollbar;

    // Start is called before the first frame update
    void Start()
    {
        onCreate();
    }

    // Update is called once per frame
    void Update()
    {

    }

    //Run this when the object is either created or activated (Basically when the dice becomes interactable)
    void onCreate()
    {
        //Insert the dice into the array
        rollbar.GetComponent<RollButton>().basicDice.Insert(diceNumber, gameObject);
    }
}

Edit: Screenshot of inspector and rollbutton script enter image description here

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class RollButton : MonoBehaviour
{

    int NumberOfDice = 0;
    public ArrayList basicDice;


    // Start is called before the first frame update
    void Start()
    {
        basicDice = new ArrayList();
    }

    // Update is called once per frame
    void Update()
    {

    }

    //Custom Function to roll the dice
    public void roll()
    {
        print(basicDice);
    }
}
DerStarkeBaer
  • 669
  • 8
  • 28
King Duck
  • 106
  • 2
  • 9
  • "I have a public GameObject that i set to another object" where? I don't see that you ever set it to anything. – D Stanley Dec 12 '19 at 20:48
  • I set it in the inspector (dragging the game object into the slot) – King Duck Dec 12 '19 at 20:51
  • Show your `RollButton` script as well as a screenshot of the inspector for the `Dice` – Draco18s no longer trusts SE Dec 12 '19 at 21:18
  • I updated the original post since im not sure how to add an image to the comments – King Duck Dec 12 '19 at 21:48
  • You have a variable named basicDice of type `ArrayList` but I don't see any code where you actually create a _new_ `ArrayList`. Where does that happen? I imagine in your `Start` method, you should have: `basicDice = new ArrayList();` Or is `basicDice` also set up using the inspector? – Chris Dunaway Dec 12 '19 at 22:09
  • i.. didnt even know you had to do that... umm, im still fairly new to unity, i started taking an online course and wanted to create my own project, im used to GMS2 so i didnt realise you needed to have the variable twice, once in the start method to initilize it im assuming? – King Duck Dec 12 '19 at 22:13
  • I created that in my start method, but its still giving the same error, it says the error is at line 28 in the Dice.cs script, which is the top set of code – King Duck Dec 12 '19 at 22:16
  • Can you show us your updated code? – Kieran Bond Dec 12 '19 at 22:26
  • Alright, updated the code with `basicDice = new ArrayList();` in the start method – King Duck Dec 12 '19 at 22:29
  • Are you 100% sure that you have the RollButton class attached to your rollbar gameobject? – Chochosan Dec 12 '19 at 23:28
  • Yes, the scipt is attached, when i was writing it, it autofilled to the name since the script was already attached to the GameObject – King Duck Dec 13 '19 at 00:50
  • You aren't guaranteed the order of the execution of Start() functions. The method that initializes the list is scheduled to run after the one that tries to add to it. Move the `new ArrayList` part next to the declaration i.e. `public ArrayList list = new ArrayList()` – Camile Dec 13 '19 at 14:01

0 Answers0