Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 14 Next capture
2021 2022 2023
2 captures
13 Jun 22 - 14 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 SirZeeno · Aug 03, 2021 at 06:17 AM · instantiatenullreferenceexception

Gameobject instantiation returns null

So i have a piece of code where i am instantiating an object to use it later as reference to other gameobjects

 public GameObject StandardEmpty;
     public string NameOfTurret;
     public bool IsDouble;
     public GameObject T_Bases;
     public GameObject T_Swivel;
     public List<GameObject> T_Mount = new List<GameObject>();
     public List<GameObject> T_Head = new List<GameObject>();
     public List<GameObject> T_Breech = new List<GameObject>();
     public List<GameObject> T_Barrel = new List<GameObject>();
     public List<GameObject> Mount_Connectors = new List<GameObject>();
     public List<GameObject> Breech_Connectors = new List<GameObject>();
     public TurretManager TurretSaveData;
     public TurretScriptable TempSavePoint;
     BuilderSync UpdateUI;
     TurretScriptable Turret;
     GameObject New_Empty;
     GameObject New_Bases;
     GameObject New_Swivel;
     GameObject New_Mount;
     GameObject New_Head;
     GameObject New_Breech_Center;
     GameObject New_Breech_Left;
     GameObject New_Breech_Right;
     GameObject New_Weapon_Barrel_Center;
     GameObject New_Weapon_Barrel_Left;
     GameObject New_Weapon_Barrel_Right;
     public Vector3 Build_Position;
     PartsAssembler PartAssembly;
     // i need to make it so it saves the part as soon as its selected not when im done creating the full turret (i think its done)
     public void Activation(int TurretIndex) //the turret index is going to tell what scriptable object index its going to save to
     {
         New_Empty = Instantiate(StandardEmpty.gameObject, Build_Position, Quaternion.identity) as GameObject;
         New_Empty.name = NameOfTurret;
         Bases(New_Empty);
         print(New_Empty.transform);
         TurretSaveData.Turrets[TurretIndex].TurretParts.Insert(0, StandardEmpty);
         TempSavePoint.TurretParts.Insert(0, StandardEmpty);
         PartAssembly.StartSync();
         ChangeChecker(TurretIndex);
     }
     /// <summary>
     /// This funtion is the activatior of this script
     /// </summary>

and i am getting a null reference at the new_empty

 void Bases(GameObject NewEmpty)
         {
             New_Bases = Instantiate(T_Bases.gameObject, Build_Position, Quaternion.identity, NewEmpty.transform) as GameObject;
             Swivel(New_Bases);
             TempSavePoint.TurretParts.Insert(1, New_Bases);
         }
         /// <summary>
         /// This funtion creates a new Bases object in the given location
         /// </summary>
         void Swivel(GameObject Bases)
         {
             Transform TSwivel = Bases.transform.Find("*SOCKET_SWIVEL");
             New_Swivel = Instantiate(T_Swivel.gameObject, TSwivel.position, Quaternion.identity, TSwivel) as GameObject;
             TempSavePoint.TurretParts.Insert(2, New_Swivel);
         }
         /// <summary>
         /// This funtion places a new Swivel object in the given socket
         /// </summary>

and that carries over to these functions which are chained and it is unclear to me why its returning as a null even thought they get spawned and they are stored in a variable, any help is appreciated.

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
0

Answer by AtiyehNorouzi22 · Aug 03, 2021 at 06:58 AM

you have to use "ref" or "out" when you want to pass your gameobject as reference not a copy of variable. you can read link text

if you want to understand why. so in your case this will solve your problem

    Bases(ref New_Empty);
 void Bases(ref GameObject NewEmpty)
          {
              New_Bases = Instantiate(T_Bases.gameObject, Build_Position, Quaternion.identity, NewEmpty.transform) as GameObject;
              Swivel(New_Bases);
              TempSavePoint.TurretParts.Insert(1, New_Bases);
          }
          /// <summary>
          /// This funtion creates a new Bases object in the given location
          /// </summary>
          void Swivel(GameObject Bases)
          {
              Transform TSwivel = Bases.transform.Find("*SOCKET_SWIVEL");
              New_Swivel = Instantiate(T_Swivel.gameObject, TSwivel.position, Quaternion.identity, TSwivel) as GameObject;
              TempSavePoint.TurretParts.Insert(2, New_Swivel);
          }
          /// <summary>
          /// This funtion places a new Swivel object in the given socket
          /// </summary>


Comment
Add comment · Show 4 · 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 SirZeeno · Aug 03, 2021 at 07:39 AM 0
Share

thanks for trying but that still didnt fix it cs it still gives me an "NullReferenceException: Object reference not set to an instance of an object" error that refers back to "Bases(ref New_Empty);"

avatar image AtiyehNorouzi22 SirZeeno · Aug 03, 2021 at 07:47 AM 0
Share

@SirZeeno because you have to add ref to swivel as well

  Bases(ref New_Empty);
      void Bases(ref GameObject NewEmpty)
               {
                   New_Bases = Instantiate(T_Bases.gameObject, Build_Position, Quaternion.identity, NewEmpty.transform) as GameObject;
                   Swivel(ref New_Bases);
                   TempSavePoint.TurretParts.Insert(1, New_Bases);
               }
               /// <summary>
               /// This funtion creates a new Bases object in the given location
               /// </summary>
               void Swivel(ref GameObject Bases)
               {
                   Transform TSwivel = Bases.transform.Find("*SOCKET_SWIVEL");
                   New_Swivel = Instantiate(T_Swivel.gameObject, TSwivel.position, Quaternion.identity, TSwivel) as GameObject;
                   TempSavePoint.TurretParts.Insert(2, New_Swivel);
               }
               /// <summary>
               /// This funtion places a new Swivel object in the given socket
               /// </summary>
 


also i couldn't understand why you find another gameobject with name after passing the gameobject as parameter in swirl Bases.transform.Find("*SOCKET_SWIVEL"); this line. also check whether the name is correct. i suspect it has star in the scene.

avatar image SirZeeno AtiyehNorouzi22 · Aug 03, 2021 at 07:55 AM 0
Share

I did but it's not referring to the swivel it's only referring to the bases. now I am not sure if this is the issue but my bases is a prefab asset of an empty with components attached to it so I don't have to add them in code. and also I am searching for an object in bases that acts as a connector between the 2 objects so they are positioned right and use the transform of that as a parent to swivel.

Show more comments

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

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

nullrefernceexception 0 Answers

Shooting Script Instantiate Problems. Help? 0 Answers

instantiate prefab object to existing object 1 Answer

Instantiating gameobjects in an array / class problem | NullReferenceException: 0 Answers

Code works in Editor but not in standalone 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