Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
2 captures
12 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 amandaadler · Mar 19, 2020 at 09:04 PM · transformgetcomponentfindgameobjectswithtag

Accessing a GameObject from another script

Hello,

I am trying to access a GameObject(Clone) from another script. I get the error:

 NullReferenceException: Object reference not set to an instance of an object
 Unit.Start () (at Assets/Unit.cs:28)

The semi-abridged code can be seen below, one script for a Spawner that creates clones of prefab'd units, the other a class to hold details on the Units :

Script SpawnUnits.cs

 public class SpawnUnits : MonoBehaviour
 {
     public GameObject[] Units;
     public static int? selectColor = null;
     public GameObject newUnit;
 
     void Awake ()
     {
         NewUnit();
     }
 
     public int NewUnit()
     {
         selectColor = Random.Range(0, 4);
         newUnit = Instantiate(Units[(int) (selectColor)], transform.position, Quaternion.identity);
         int? selectColor1 = (selectColor);
         return (int)selectColor1;
     }
 }

Script Unit.cs

 public class Unit : MonoBehaviour {
 
     public GameObject SpawnGameObject;
     private SpawnUnits SpawnScript;
 
     public Transform t;
 
     [System.NonSerialized]
     public int TileColor;
 
     // The point around with the game pieces rotate
     public Vector3 rotationPoint;
     // How fast the piece floats up naturally (with no key presses)
     public float floatTime = 0.8f;
 
     void Awake () 
     {
         // t = GetComponent<Transform>();
     }
 
     void Start ()
     {
         SpawnScript = SpawnGameObject.GetComponent<SpawnUnits>();
         Debug.Log(SpawnScript);
         t = SpawnScript.newUnit.transform;
     }
 
 }

Debug logs show SpawnScript as null, therein not picking up the reference properly. I'm trying to store the transform of these GameObject clones, but to no avail. I'm having awful difficulty understanding how to properly build out inter-script relationships, so any help is appreciated!

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 metalted · Mar 19, 2020 at 09:35 PM 0
Share

Im just going to focus on the start function for now. In the first line you try to set "SpawnScript" to a component taken from "SpawnGameObject". What could go wrong: A. SpawnGameObject is not assigned (this would throw a null reference exception). B. SpawnGameObject doesnt have a component SpawnUnits so it returns null. If the Debug.Log() already gives null, the problem is above that Debug.Log().

1 Reply

· Add your reply
  • Sort: 
avatar image
0

Answer by Zentiu · Mar 20, 2020 at 01:21 AM

Tell me if im wrong but i am assuming that the unit script is attached to the unit gameobject you spawn with the spawnunits script. Since you already get a reference of the new unit during instantiation (newUnit = Instantiate(Units[(int) (selectColor)], transform.position, Quaternion.identity);) There you could reference the script like:

 newUnit.GetComponent<Unit>().t = newUnit.transform;

Metalted is correct. You need to set the SpawnGameObject. SInce the new unit is probebly a prefab that got instantiated it will not hold a reference to spawnGameObject since prefabs dont hold any references that are in the hierarchy of your scene unless its part of the prefab itself. You can look for the gameobject with the 'SpawnUnits' through tags or gameobject.find. The code in awake of your unit script should also work or just move it to the start method.

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

160 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 avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

How getting transform of another GameObject C#? 1 Answer

modifing enemies target inside a radius 1 Answer

How to use "GetComponent" and "transform" code in dll file? 0 Answers

Print current Transform of an instance in the debug log 1 Answer

So here is some really complicated question. 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