Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 11 Next capture
2021 2022 2023
1 capture
11 Jun 22 - 11 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 Ultrapilpo · Aug 08, 2015 at 12:25 PM · childactivatedeactivate

How to activate the child of an object without declaring a public transform?

My script is pretty simple. It involves a stick which I want to pick up, so when I walk into the box collider it is triggered and if I press E the stick is destroyed.

The problem is that I have a small floating 'E' above the stick so that the player knows which button to press in order to pick up the stick, this 'E' appears when you walk into the collider and is a child of the stick.

So when I destroy the stick I get an error saying that I am still trying to access the object after I have destroyed it. This is because I have publicly declared the child transform. Is there another way to setActive() the child of the stick without having to publicly declare the transform?

Here is my code......

using UnityEngine; using System.Collections;

public class PlayerInv : MonoBehaviour {

 public GameObject interactIcon;
 
 
 void OnTriggerStay(Collider col){
     if(col.gameObject.CompareTag("Stick"))
     {
         interactIcon.gameObject.SetActive (true);
         if(Input.GetKeyDown(KeyCode.E))
         {
             Destroy(col.gameObject);
         }
     }
 }
 
 void OnTriggerExit(Collider col){
     if(col.gameObject.CompareTag("Stick"))
     {
         interactIcon.gameObject.SetActive (false);
     }
     Debug.Log ("exiting collider");
 }

}

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 getyour411 · Aug 08, 2015 at 02:56 PM 0
Share

set the reference to null then destroy()

avatar image stepan-stulov · Aug 08, 2015 at 04:33 PM 0
Share

Quote: So when I destroy the stick I get an error saying that I am still trying to access the object after I have destroyed it.

I might sound like a CO, but don't access it. It's now destroyed. But to be serious: I would inject the E marker of the stick into the stick script. So when you enter a collider which you are sure is associated with a stick (check it either against a tag, a name, a component presence, etc.), get the stick component from the collider's game object and do whatever you're pleased with it inlcluding messing with marker. If the stick is destroyed - its collider won't trigger, so there is no stick and therefore no marker to access from. You have a rather logical problem, as you mess with dependencies in an unconventional way. The marker belongs to the stick, so why is it treated as it's a standalone object being able to exist without the stick. Why do you inject it into your player script bypassing the stick-owner. Decide on main participants of you gameplay/simulation first and it will be clear what to inject where, and who belongs to whom. Program it as if there is no marker first, just the player and the stick. And then add methods Show$$anonymous$$arker() and Hide$$anonymous$$arker() to the stick managing the marker injection internally in the stick script.

1 Reply

· Add your reply
  • Sort: 
avatar image
0

Answer by Owen-Reynolds · Aug 08, 2015 at 05:45 PM

" error [...] because I have publicly declared the child transform."

No. That's not true. It's like saying your fingers are smashed because your car has doors. The real problem is that you're using it wrong.

I think stepan_stulov is correct. Are you destroying the E marker? Pause the game and look for it. Do you want to destroy it? If not, how can you avoid it's destruction?

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

4 People are following this question.

avatar image avatar image avatar image avatar image

Related Questions

Most effective way to get deactivated children? 1 Answer

Destroying a child object? 1 Answer

Activate/deactivate all GameObjects with same tag c# 1 Answer

What happens to the code of the Start/Awake function when I deactivate a gameobject? 1 Answer

How To Disable A Prefab? 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