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 MrSmarticles · Jan 27, 2017 at 06:29 PM · c#scripting problemscripting beginnerprefabsclasses

instantiating unique classes of a type

I am trying to make a grand strategy game on the scale of planets. For this I want to be able to generate a semi randomised "planet" with a set of properties attached via a script.

atm my planet class looks like this:

 using System;
 using System.Collections;
 using System.Collections.Generic;
 using UnityEngine;
 
 public class Planet : MonoBehaviour  {
     public int population;
     public int Population {
         get {
             return population;
         }
         set {
             value = population;
         }
     }
     public int space;
     public int Space
     {
         get
         {
             return space;
         }
         set
         {
             value = space;
         }
     }
     public double  survivability;
     public double Survivability
     {
         get
         {
             return survivability;
         }
         set
         {
             value = survivability;
         }
     }
     public string type;
     public string Type
     {
         get
         {
             return type;
         }
         set
         {
             value = type;
         }
     }


I run in to problems when i am trying to attach a new planet script to the prefab when it is instantiated ive tried doing it like this:

 using System.Collections;
 using System.Collections.Generic;
 using UnityEngine;
 
 
 public class PlanetGen : MonoBehaviour {
 
     int population;
     int space;
     string type;
     double survivability;

     public static void CreatePlanet(GameObject go, int population, int space, string type, double survivability)
     {
 
         Planet newPlanet = go.AddComponent<Planet>();
         newPlanet.Population = population;
         newPlanet.Space = space;
         newPlanet.Type = type;
         newPlanet.Survivability = survivability;
 
         newPlanet.name = go.name;
     }

     void Start () {
         
         System.Random r;
 
         r = new System.Random();
 
         space = r.Next(1, 100);
 
         int di_Type = r.Next(0, 3);
 
         switch (di_Type)
         {
             case 0:
                 type = "Terrestrial";
                 survivability = 1;
                 break;
             case 1:
                 type = "Lava";
                 survivability = 0.4;
                 break;
             case 2:
                 type = "Gaia";
                 survivability = 1.1;
                 break;
         }
 
         population = (int)System.Math.Round(0.25 * space * survivability);
 
         CreatePlanet(gameObject, population, space, type, survivability);
     }
 

 
 }
 

But from this I am stuggling to find a way to make it so a script attached to the same object as this new instance can access and edit the variables. I would really like to be able to do it this way so that large random maps can be generated easily but if this method isn't gonna work id love to know. Ive looked around for hours but none of the refrences ive found really address what i want to do.

here's an example of what i would like to be able to do with my planet scripts once they're attached bare in mind I'm trying to get these scripts to be generalised in a sense so they will work on any instance of the prefab:

 public class PlanetGrowth : MonoBehaviour {
 
     float population;
    
 
     void Start () {
 
         InvokeRepeating("GrowPop", 2.0f, 10.0f);
 
     }
 
     void GrowPop() {
 
         population = gameObject.GetComponent<Planet>().population;
 
         if( population < gameObject.GetComponent<Planet>().Space ){
 
         population = population + 1 + (1*(float)gameObject.GetComponent<Planet>().Survivability);
         gameObject.GetComponent<Planet>().Population = (int) Mathf.Round(population);
 
         print(population);
         }
     }

 

edit: atm an empty copy of the planet class is attached at least that is how it appears on the inspector at runtime

Comment
Add comment · Show 1
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 ThomLaurent · Jan 27, 2017 at 06:39 PM 0
Share

It seems to be a ti$$anonymous$$g problem :

If Unity doesn't call your PlanetGen.Start() method before the PlanetGrowth.Start() one you'll try to access a Planet which is not already created.

Do you have any exception ?

For the design you want I would suggest your PlanetGen script to create a new GameObject per new Planet, so basically a Planet IS a GameObject

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

8 People are following this question.

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

Related Questions

How to move the object to where the object is already pointing to? 1 Answer

How do I make a crouch script unity 3d 1 Answer

Enemy Health Script Working On One Enemy Not Multiples 2 Answers

Help with ontrigger enter and exit with UI Display appear and disappear 1 Answer

I'm trying to allow my character to only jump when grounded 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