Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 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 Genero · Oct 10, 2017 at 09:33 AM · instantiateclassmonobehaviourienumerator

Custom class with coroutine can't be added using the "new" keyword

As the title suggests, the error is obvious but the workaround is not. I have an RTS-style game where, in the gameworld, there are resource nodes. The resource nodes have individual spawnpoints onto which the resources themselves can spawn. Whenever an NPC wishes to go and retrieve some of these resources, a reservation request is sent and stored in a "Reservation" object. This Reservation object is a custom class, which holds a reference to the NPC who made the reservation, as well as a list of the spawnpoints that were reserved.

HOWEVER, built into this Reservation object, I have a method for removing the first entry on the list of spawnpoints. Accessing this function is no problem, but I wanted to introduce a delay so that it does not happen immediately. For this I built an IEnumerator which simply waits for a specified delay before calling this entry removal function. For this IEnumerator to work, I had to inherit my Reservation class from MonoBehavior.

But this meant that I could no longer initialize my newly added reservations using the New keyword. Here's the code I use to illustrate my problem. First there's the class in question:

     public class Reservation : MonoBehaviour
     {
         //The Wisp who made the reservation
         public WispScript WispScriptRef;
         //A list of the resource node spawnpoints whose resources were reserved to this wisp
         public List<RNodeSpawnpoint> RNodeListSpawnpointList = new List<RNodeSpawnpoint>();
 
         //Used to indicate whether a Wisp is currently in transition between resource spawnpoints
         public bool ReservationExecuting = true;    //Initialized to true, as we know someone made the reservation
 
         //Add the passed Spawnpoint to the Reservation's list of spawnpoints
         public void MakeReservation(RNodeSpawnpoint SpawnpointToReserve)
         {
             RNodeListSpawnpointList.Add(SpawnpointToReserve);
         }
 
         //Remove the last element in the list
         public void RemoveLastReservation()
         {
             RNodeListSpawnpointList.RemoveAt(0);
         }
         //Call RemoveLastReservation at a delay
         public IEnumerator DelayedRemoveLastReservation(int DelayTime)
         {
             yield return new WaitForSeconds(DelayTime);
             RemoveLastReservation();
         }
         //Allow external scripts to call the IEnumerator above
         public void CallDelayedRemoveLastReservation(int DelayTime)
         {
             StartCoroutine(DelayedRemoveLastReservation(DelayTime));
         }
     }

Here's the line I use to create a list to contain these objects:

     //Holds a reference to all of our reservation-objects
     public List<Reservation> ReservationList = new List<Reservation>();

And here's the line I use to add items to that list, which is where I believe that the problem might be occurring:

         Reservation NewReservation = new Reservation();

Right after that line of code, I simply initialize the member variables inside the Reservation object before I then add it to the list of Reservation objects via:

 ReservationList.Add(NewReservation);

Unity suggests I use AddComponent() instead, but how would I go about getting the same result then? All I want is to have a list of the Reservation objects.

I'm considering moving the IEnumerator out of the Reservation class and simply having the object making these calls, do the waiting instead. It would solve my problem, but it would decentralize my code in a way that is not optimal for my design standard and add unnecessary steps to the production chain later.

Thanks in advance!

  • Dave

Comment
Add comment
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

1 Reply

· Add your reply
  • Sort: 
avatar image
1

Answer by Hellium · Oct 10, 2017 at 09:50 AM

Scripts inheriting from MonoBehaviours can't be instantiated using the new keyword.

You have two possibilities :

  1. Remove the inheritance in the Reservation class. If a Reservation instance is not meant to be attached on a gameobject, it's the way to go. However, you won't be able to start a coroutine within this class (but you can leave the coroutine definition and run from an other MonoBehaviour)

  2. Use the following snippet :

     Reservation NewReservation = new GameObject("Resevation").AddComponent<Reservation>();
    
    
    
Comment
Add comment · 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

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

88 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 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 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 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 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 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

Can I call a class's method which inherit monobehaviour by a normal class? 0 Answers

Alternative to using prefabs w/scripts to save sets of data for use at runtime? 1 Answer

Threads for Instantiate 1 Answer

C# Question - Do I Have to inherit from MonoBehaviour? What happens if I don't? 2 Answers

List of Monobehaviour clasess, Editor 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