• 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 /
  • Help Room /
avatar image
Question by Al3xQQ · Mar 29, 2017 at 03:08 PM · instantiatenull reference exception

Getting error NullReferenceException on instantiate

T$$anonymous$$s morning all worked so fine and know when i wanted to continue my project i'm getting an error. I work with collab and i got an error earlier and needed to chage turret to Turret to can update the project to collab. P.S. Script node.cs color the hover mouse node only if you clicked a button before that. That worked in morning but now i the hover color of node appear without you to click that button. I don't understand. I'm mind f**k... I'm getting t$$anonymous$$s error:

 UnityEngine.Object.Internal_InstantiateSingle (UnityEngine.Object data, Vector3 pos, Quaternion rot) (at C:/buildslave/unity/build/artifacts/generated/common/runtime/UnityEngineObjectBindings.gen.cs:51)
 UnityEngine.Object.Instantiate (UnityEngine.Object original, Vector3 position, Quaternion rotation) (at C:/buildslave/unity/build/Runtime/Export/UnityEngineObject.cs:157)
 UnityEngine.Object.Instantiate[GameObject] (UnityEngine.GameObject original, Vector3 position, Quaternion rotation) (at C:/buildslave/unity/build/Runtime/Export/UnityEngineObject.cs:207)
 BuildManager.BuildTurretOn (.Node node) (at Assets/Scripts/BuildManager.cs:29)
 Node.OnMouseDown () (at Assets/Scripts/Node.cs:35)
 UnityEngine.SendMouseEvents:DoSendMouseEvents(Int32)

Node.cs using UnityEngine; using UnityEngine.EventSystems;

 public class Node : MonoBehaviour {
     [Header("Optional")]
     public GameObject turret;
     public Color hoverColor;
     public Vector3 positionOffset;
     private Renderer rend;
     private Color startColor;
     BuildManager buildManager;
 
     void Start()
     {
         rend = GetComponent<Renderer> ();
         startColor = rend.material.color;
         buildManager = BuildManager.instance;
     }
 
     public Vector3 GetBuildPosition()
     {
         return transform.position + positionOffset;
     }
 
     void OnMouseDown()
     {
         if (EventSystem.current.IsPointerOverGameObject ())
             return;
         if (!buildManager.CanBuild)
             return;
         if (turret != null) {
             Debug.Log ("Ai creat deja o arma aici!");
             return;
         }
         buildManager.BuildTurretOn (t$$anonymous$$s);
     }
 
     void OnMouseEnter()
     {
         if (EventSystem.current.IsPointerOverGameObject ())
             return;
         if (!buildManager.CanBuild)
             return;
         rend.material.color = hoverColor;
     }
 
     void OnMouseExit()
     {
         rend.material.color = startColor;
     }
 }

BuildManager.cs

 using UnityEngine;
 
 public class BuildManager : MonoBehaviour {
 
     public static BuildManager instance;
 
     void Awake()
     {
         if (instance != null) {
             Debug.Log ("More than one BuildManager in scene!");
             return;
         }
         instance = t$$anonymous$$s;
     }
     public GameObject standardTurretPrefab;
     public GameObject anotherTurretPrefab;
 
     public TurretBluePrint turretToBuild;
 
     public bool CanBuild { get { return turretToBuild != null; } }
 
     public void BuildTurretOn(Node node)
     {
         if (PlayerStats.Money < turretToBuild.cost) {
             Debug.Log ("Nu ai bani destui!");
             return;
         }
         PlayerStats.Money -= turretToBuild.cost;
         GameObject turret = (GameObject)Instantiate (turretToBuild.prefab, node.GetBuildPosition(), Quaternion.identity);
         node.turret = turret;
         Debug.Log("Turret build! Money left: " + PlayerStats.Money);
     }
 
     public void SelectTurretToBuild(TurretBluePrint turret)
     {
         turretToBuild = turret;
     }
 }

TurretBluePrint.cs

 using System.Collections;
 using System.Collections.Generic;
 using UnityEngine;
 
 [System.Serializable]
 public class TurretBluePrint{
 
     public GameObject prefab;
     public int cost;
 
 }

The errors from scripts:

BuildManager.cs Line 29:

 GameObject turret = (GameObject)Instantiate (turretToBuild.prefab, node.GetBuildPosition(), Quaternion.identity);



Node.cs Line 35:

 buildManager.BuildTurretOn (t$$anonymous$$s);
Comment

People who like this

0 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 hexagonius · Mar 29, 2017 at 05:01 PM 0
Share

maybe something went wrong with the serialisation. This kind of error doesn't look normal. I'd assign prefab to turretToBuild again.

avatar image Rhombuster · Mar 29, 2017 at 09:26 PM 0
Share

Is this right?

 public class TurretBluePrint

Should this have : MonoBehaviour on it?

3 Replies

· Add your reply
  • Sort: 
avatar image

Answer by Al3xQQ · Mar 30, 2017 at 04:21 PM

@hexagonius How and where should i do that?

@Rhombuster No, that's how shoud be. https://youtu.be/pZ0QyngaQv4?t=1m31s

Comment

People who like this

0 Show 1 · 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
avatar image DilianaS · May 07, 2022 at 11:06 AM 0
Share

Hi. @Al3xQQ ,

did you managed to sollte the problem?

avatar image

Answer by MarnixM · Apr 02, 2017 at 03:55 PM

so i have the same idea for a game like you do. have u checked the shop script in the inspector? because you might have failed the fill in the prefabs for standardTurret and missileTurret. i had the same error and after checking everyt$$anonymous$$ng and remaking all the scripts, I found out that it had to do with the blank space under the shop script in the inspector. just fill in these spots with the prefabs and your game will work again.

Comment

People who like this

0 Show 0 · 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
avatar image

Answer by Shiner252 · Jul 24, 2017 at 09:57 PM

I had the same problem. I might be late, but I will answer in case someone stumbles upon t$$anonymous$$s w$$anonymous$$le searc$$anonymous$$ng for a solution. In your Node.cs script, make sure you define the instance of the Build Manager like so:

void Start(){ buildManager = BuildManager.instance }

Comment

People who like this

0 Show 0 · 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.

Welcome to Unity Answers

If you’re new to Unity Answers, please check our User Guide to help you navigate through our website and refer to our FAQ for more information.

Before posting, make sure to check out our Knowledge Base for commonly asked Unity questions.

Check our Moderator Guidelines if you’re a new moderator and want to work together in an effort to improve Unity Answers and support our users.

Follow this Question

Answers Answers and Comments

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

Why cant i instantiate my ship? Error NullReferenceException: Object reference not set to an instance of an object. 1 Answer

NullReferenceException problems. 0 Answers

Object does not instantiate, causing an error while attempting to get a component. 2 Answers

Object reference not set message, directly after instantiation 1 Answer

Getting 'Object Reference not set' when calling function from instantiated 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