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 /
This question was closed Mar 21, 2017 at 07:00 PM by MegaChuck64 for the following reason:

The question is answered, right answer was accepted

avatar image
0
Question by MegaChuck64 · Mar 21, 2017 at 05:06 PM · instantiateinheritanceresources.load

Instantiating from an object classes GameObject property?

Right now I have a base class called Tile that doesn't derive from monobehaviour. It has a private GameObject with public getter and setter. I have another class called StoneTile that inherits from Tile where I set the GameObject via Resouces.Load();. Then I have an actual game script which creates an instance of StoneTile and instantiates StoneTile's GameObject... But it causes a NullRefrenceException. I'm trying this new method of clean code and inheritance so I'm a bit lost. Any tips or ideas will help. Thanks

 using UnityEngine;
 using System.Collections;
 
 public class Tile
 {
     private string type = "TILE";
     private float speedBoost = 0f;
     private float jumpBoost = 0f;
     private float clickIncrease = 0.1f;
     private float naturalIncrease = 0.1f;
     private GameObject go;
 
 
     public string Type
     {
         get
         {
             return type;
         }
         set
         {
             type = value;
         }
     }
     
     public float SpeedBoost
     {
         get
         {
             return speedBoost;
         }
         set
         {
             speedBoost = value;
         }
     } 
 
     public float JumpBoost
     {
         get
         {
             return jumpBoost;
         }
         set
         {
             jumpBoost = value;
         }
     }
 
     public float ClickIncrease
     {
         get
         {
             return clickIncrease;
         }
         set
         {
             clickIncrease = value;
         }
     }
 
     public float NaturalIncrease
     {
         get
         {
             return naturalIncrease;
         }
         set
         {
             naturalIncrease = value;
         }
     }
 
     public GameObject GO
     {
         get
         {
             return go;
         }
         set
         {
             go = value;
         }
     }
 }

~~~~~

 using UnityEngine;
 using System.Collections;
 
 public class StoneTile : Tile
 {
     public StoneTile()
     {
         Type = "Stone";
         SpeedBoost = 0.2f;
         JumpBoost = 0;
         ClickIncrease = 0.2f;
         NaturalIncrease = 0.1f;
         GO = Resources.Load("Stone") as GameObject;
     }
 }

~~~~

 using UnityEngine;
 using System.Collections;
 
 public class WorldCreate : MonoBehaviour {
 
     
     public int width;
     public int height;
     public Tile[,] map;
      
 
     void Start()
     {
         map = new Tile[width, height];
         GenerateMap();
         Initialize();
     }
 
 
     void GenerateMap()
     {
         for (int x = 0; x < width; x++)
         {
             for (int y = 0; y < height; y++)
             {
                 float r = Random.value;
                 if (r <= 0.25f)
                 {
                     map[x, y] = new StoneTile();
                 }
             }
         }
     }
 
     void Initialize()
     {
         for (int x = 0; x < width; x++)
         {
             for (int y = 0; y < height; y++)
             {
                 GameObject newTile = Instantiate(map[x,y].GO);    ***<---- Error Here***
                 newTile.transform.position = new Vector3(x, 0, y);
             }
         }
     }
 }
 

Comment
Add comment · Show 2
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 Hellium · Mar 21, 2017 at 05:09 PM 1
Share

Can you post the script where the error occurs please ? Also, indicate exactly the line causing the error.

avatar image MegaChuck64 Hellium · Mar 21, 2017 at 05:16 PM 0
Share

Oops. Yes, sorry about that. I accidentally posted the same script twice. The script with the error is the last one. There is an arrow that indicates where the error is. Thanks for pointing that out

1 Reply

  • Sort: 
avatar image
0
Best Answer

Answer by MegaChuck64 · Mar 21, 2017 at 06:59 PM

Well I got the answer from reddit you slackers! The problem was that not all Tiles in the map array were getting set so when it tried to instantiate a null Tile it would just stop running instead of simply skipping it. A simple null check fixes the problem.

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

Follow this Question

Answers Answers and Comments

78 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

Related Questions

Problem with missing texture with Instantiate( Resources.load ) method 0 Answers

Instantiate a class 0 Answers

Class Organization and Inheritance 1 Answer

which is better : putting all files(Images, AudioClips, ...) in Resources and make one generic prefab ? OR make many Prefabs and instantiate them in Runtime ? and why?! 0 Answers

bind instantiated particles to an object 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