Hello!
I have two classes: GameEvent is a container for text-based game events. GameEventManager is for event generation and handling. I’m trying to fill a List with 10 randomly generated events. But “creating a MonoBehaviour using the ‘new’ keyword is not allowed”. I googled this problem, but still don’t understand how should I do it correctly. As a result, I want to see 10 runtime-generated events in the inspector in editor
Can someone point me in the right direction? Thanks!
using UnityEngine;
using System.Collections;
public class gameEvent : MonoBehaviour {
public string description;
public int id;
public string image;
public string callToAction1;
public string callToAction2;
public int nextEventID;
public gameEvent(int newId, string newImage, string newDescription)
{
id = newId;
image = newImage;
description = newDescription;
outputEventInfo();
}
public void outputEventInfo()
{
Debug.Log("Event " + id + ": " + description + " Actions: " + callToAction1 + ", " + callToAction2 + " (Image: " + image);
}
}
using UnityEngine;
using System.Collections;
using System.Collections.Generic;
public class GameEventManager : MonoBehaviour {
public List<gameEvent> events = new List<gameEvent>();
public int eventCounter = 0;
// Use this for initialization
void Start () {
for (eventCounter = 0; eventCounter < 5; eventCounter++) {
GenerateEvent();
}
}
public void GenerateEvent()
{
events.Add(new gameEvent(eventCounter, "image_" + eventCounter, "Some test event is happening"));
}
}
A MonoBehaviour is the type of script you attach to a GameObject in Unity. You can’t instantiate these on their own, only attach them to an existing GameObject. Are you sure you want them to inherit from MonoBehaviour?
Thank you! Finally I’ve got a working piece of code. As I found out later, you can’t use class constructors while instantiating MonoBehaviour object. But you can use a separate function for initialization (or make static method for custom class which will return new gameObject with attached script and some initialization data).
If someone will stuck with this like I did:
using UnityEngine;
using System.Collections;
public class gameEvent : MonoBehaviour{
public string description;
public int id;
public string image;
public void generateEvent(int newId, string newImage, string newDescription)
{
id = newId;
image = newImage;
description = newDescription;
}
}
using UnityEngine;
using System.Collections;
using System.Collections.Generic;
public class GameEventManager : MonoBehaviour {
public List<GameObject> events = new List<GameObject>();
public int eventCounter = 0;
// Use this for initialization
void Start () {
for (eventCounter = 0; eventCounter < 5; eventCounter++) {
GenerateEvent();
}
}
public void GenerateEvent()
{
var e = new GameObject();
e.AddComponent<gameEvent>().generateEvent(eventCounter, "image_" + eventCounter, "Some test event is happening");
events.Add(e);
}
}
On second thought, this is still not what I want. Now I’ve got some junk objects in my scene.
I have another script, which behaves just perfect and all extra objects can be seen in Inspector:
I tried to replicate this class with my new event system - made serializable simpe class… and suddenly it’s working just fine. Wow. I have no idea why this solution didn’t worked for me yesterday.
using UnityEngine;
using System.Collections;
[System.Serializable]
public class gameEvent{
public string eventName;
public string description;
public int id;
public string image;
public void generateEvent(int newId, string newImage, string newDescription)
{
eventName = "Event " + newId;
id = newId;
image = newImage;
description = newDescription;
}
}
using UnityEngine;
using System.Collections;
using System.Collections.Generic;
public class GameEventManager : MonoBehaviour {
public List<gameEvent> events = new List<gameEvent>();
public int eventCounter = 0;
void Start () {
for (eventCounter = 0; eventCounter < 5; eventCounter++) {
GenerateEvent();
}
}
public void GenerateEvent()
{
gameEvent e = new gameEvent();
e.generateEvent(eventCounter, "image_" + eventCounter, "Some test event is happening");
events.Add(e);
}
}