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 Nobozarb · Aug 06, 2017 at 07:00 PM · scripting problemprefabsinstances

GameObject.Find() not working when connected to prefab

I'm creating a game involving powerups. To spawn these powerups, I have a prefab of a powerup that is instantiated into the game every few seconds. The powerup I am currently testing is a powerup that spawns the walls around the edge of the screen when collided with. These walls prevent you from knocking your opponent off the platform and winning. However, it appears that only one instance of my prefab is usable at any one time, so colliding with a powerup will frequently not do anything (except give an error message) and leave the powerup there. The error message reads:

 NullReferenceException: Object reference not set to an instance of an object
 WallPowerup.OnTriggerEnter (UnityEngine.Collider other) (at Assets/Scripts/WallPowerup.cs:21)

The line numbers pointed to give me reason to suspect this block of code:

 void OnTriggerEnter (Collider other)
     {
         if (other.gameObject.CompareTag ("Player") || other.gameObject.CompareTag ("Comp")) {
             walls.SetActive (true);
             gameController.StartCoroutine (gameController.DestroyWalls ());
             Destroy (this.gameObject);
         }
     }

The walls variable is a GameObject set in the Start() function, with GameObject.Find(), and the line walls.SetActive(true) is the specific line the error message points to. Since the rest of OnTriggerEnter() isn't functioning either, I think whatever error is going on manages to stop this specific instance of the script, but not any of the scripts in other instances of the same prefab.

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 ShadyProductions · Aug 06, 2017 at 07:30 PM 0
Share

Show us the find code

2 Replies

· Add your reply
  • Sort: 
avatar image
0
Best Answer

Answer by unidad2pete · Aug 06, 2017 at 07:52 PM

GameObjects set Inactive when you use GameObject.Find() are not found.

try start scene with object activated, then add new line to your start function.

 void Start()
 {
      walls = GameObject.Find("GAME OBJECT NAME");
      walls.SetActive(false);
 }

Other way to find gameObjects inactived on scene is searching a child Transform inactived on a active gameObject, gameObjects not active in scene are not found, but transform childs not active do it.

 public GameObject wallParent;   // EMPTY ACTIVE GAME OBJECT
     public GameObject wall;         // YOUR OBJECT, CHILD OF wallParent AND NOT ACTIVE
     void Start ()
     {
         wallParent = GameObject.Find("WALL PARENT NAME");
         wall = wallParent.transform.Find("YOUR GAME OBJECT NAME").gameObject;
     }
Comment
Add comment · Show 3 · 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 Nobozarb · Aug 07, 2017 at 05:28 PM 0
Share

Are you saying that I can't use GameObject.Find() on inactive objects? In that case, I could access the walls variable from a script that has a value for the walls variable before they are deactivated?

avatar image unidad2pete Nobozarb · Aug 09, 2017 at 05:12 PM 0
Share

Yes, you cant found inactive objects with GameObject.Find(), but you can get acces all scripts when you have the object reference.

I mean, if you find an inactive object, you recive null and next code lines not continues. But if you found the object , dont care if the object is active or not, you have the reference to the object, and you can acces all scripts.

avatar image Nobozarb unidad2pete · Aug 09, 2017 at 05:39 PM 0
Share

$$anonymous$$y updated script works now. Thanks for the help!

avatar image
0

Answer by faithmorante · May 15 at 09:12 AM

I'm super late to the party but transform.Find works on inactive objects. So what you can do is nest the inactive objects in an empty Game Object, then find that parent GameObject then find the inactive object

 GameObject.Find("Main").transform.Find(gameObject).gameObject.SetActive(true);
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

111 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

Related Questions

Counting prefabs in screen?,Problem with counting prefabs in screen 1 Answer

how do I assign a prefab's child to another prefab's script's public variable? 1 Answer

Running scripts from editor 1 Answer

Problems calling functions from Scriptable Objects 0 Answers

Problem with multiple instances of script displaying same text object 2 Answers


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