- Home /
How to add an array to monobehaviour
Ok, so to be as specific as possible, I am trying to make a script without monobehaviour. So, now that i changed it from MonoBehaviour to none, Some of my code is invalid, which i tottally get. For example Look at this script and you can see what is wrong with it
using UnityEngine;
using UnityEngine.UI;
public class Idontknow {
public GameObject Prefab;
public float Cost;
public int money;
// Now this is the part where it's invalid and i tottally get it.
void Start () {
money = 0;
moneyText.text = money.ToString();
}
void Update {
if (Input.GetKeyDown("w")){
money += 1;
}
}
}
As you can see, the voids and the text is invalid. So the way im using this script right now, is i said on my other script this:
public Idontknow[] Guns;
Now, the only problem is, when i make the "Idontknow" script back to monobehaviour it wants it as a script istelf, a gameobject. Now i guess i could do this but i really dont want to because the script "I dont know" is not supposed to be aattached with the other script. Would there be anyway around this?
Answer by Timo326 · Feb 05, 2018 at 09:56 PM
Even if it is not a monobehaviour you need anywhere create an object of your class. Without beeing a monobehaviour your update function will not update every frame automatically. MoneyText is invalid because there is no definition for it. Beeing you I would create an empty gameobject and attach the script as monobehaviour. I hope this helps a little.
How would i do that? add it as $$anonymous$$onoBehaviour ? or something? and yes, i know i forgot to add moneytext there. Be more specific?
Your problem is that you get compiler errors? Just saw that you also forgot the brackets () after void Update.
Sorry for late response, but there is no monobehvaiour would that effect it?
Your answer
Follow this Question
Related Questions
Jagged Array returns null anywhere outside function that populates it 1 Answer
How do i give Coordinates (x, y) to an Array 1 Answer
How to generate a random int that does not repeat consecutively? 1 Answer
Multiple Cars not working 1 Answer
HELP!!! This NullReferenceException won't go away! :( 1 Answer