Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 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
1
Question by bob1234 · Jul 28, 2014 at 12:04 AM · gameobjectsetactive

game object set active on trigger enter?

I'm trying to make a jetpack pick up object, so that when you run into the jetpack that is laying on the ground, it destroys from the ground, and then i want to set my jetpack gameobject to active, that i have on my player, which ive set to false as default so its not showing. Ive got the code attached to the jetpack object, but its not working, just giving me a null reference exception, am i missing getcomponent somewhere? please help, thx :)

Heres my code as following:

 void OnTriggerEnter2D(Collider2D other)
     {
         if (other.tag == "Player") 
         {
             GameObject go = GameObject.FindGameObjectWithTag ("JetPack");
             go.SetActive (true);
             Destroy (gameObject);
         }
         
     }
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 bob1234 · Jul 28, 2014 at 12:39 AM 0
Share

Wow, weird i didnt think of that! that worked perfectly, thanks alot man :) except i changed the code abit and did GameObject.FindGameObjectWithTag ("JetPack").renderer.enabled = false; in the awake and then GameObject.FindGameObjectWithTag("JetPack").renderer.enabled = true; in the ontrigger method :P thanks again!

2 Replies

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

Answer by Hexer · Jul 28, 2014 at 12:33 AM

I don't think it is possible to activate an object when it is inactive (sound stupid but I tried it out) the gameobject has to be checked to let the script work.

Instead you could just de-activate the render of the jetpack on the player and when the player collide with the jetpack on the ground the render is set to true.

 void OnTriggerEnter2D(Collider2D other)
     {
         if (other.tag == "Player") 
         {

             GameObject.Find("JetPack").GetComponent(Renderer).enabled=true;

 //activate the JetpackScript
 gameObject.GetComponent<TheJetpackScript>().enabled = true;   



      // Destroys the jetpack that is on the ground 
             Destroy (gameObjectWithTag("JetpackOnTheGround"));
         }
  
     }


Comment
Add comment · 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 bob1234 · Jul 28, 2014 at 12:40 AM 0
Share

Wow, weird i didnt think of that! that worked perfectly, thanks alot man :) except i changed the code abit and did GameObject.FindGameObjectWithTag ("JetPack").renderer.enabled = false; in the awake and then GameObject.FindGameObjectWithTag("JetPack").renderer.enabled = true; in the ontrigger method :P thanks again! (just repasted the comment) i will set this as answer, thanks :)

avatar image
0

Answer by RoyalDio2 · Apr 04, 2016 at 09:07 AM

using UnityEngine; using System.Collections;

public class PlayerController : MonoBehaviour {

 public GameObject cubes;// Object to active
 public float sec = 14f;


 void OnTriggerEnter(Collider other)
 {
     if (other.tag == "JetPack") { //tag name Object to Pick Up
         
         cubes.SetActive (true);
         StartCoroutine (LateCall());
         Debug.Log ("JetPack Ready");

     }
 }

 //after same sec Object to false

 IEnumerator LateCall()
 {

     yield return new WaitForSeconds (sec);
     cubes.SetActive (false);
     Debug.Log ("JetPack Diactive!");
 }


}

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

5 People are following this question.

avatar image avatar image avatar image avatar image avatar image

Related Questions

Problems with save 1 Answer

ReActivate object with null movement ?? 2 Answers

Need workaround for this line of code 1 Answer

GameObjects that I deactivate are reactivated immediately 0 Answers

c# Set Active error, conflicting with other code.. 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