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 superventure · Mar 21, 2011 at 11:46 PM · characteractivatedeactivate

Activating things- What on earth am I doing wrong???

Hey unity people! I am trying to activate and deactive a character on gui button clicks.

I am using Npc.active = true/false; , but when I deactivate it it only freezes the person. When I try to reactive it it tells me that there's a null reference exception...

I wan't to be able to completely turn off/on (not destroy) the npc and its children just like you can in the inspector window when you check the button for the object.

I have tried using npc.SetActiveRecursively(true) and its not working either. What am I doing wrong?

I am calling 'npc' with this

function Page4() {

Buttons();

npc = GameObject.FindWithTag("NPCmain");

GUI.Box (Rect (200,480,500,105), Name +"'s Team");

if (GUI.Button(Rect(210, 496, 150,30), " recall person1 ")){ npc.transform.position = transform.position + transform.right * -4; npc.transform.parent = transform; npc.active = false;

}if (GUI.Button(Rect(210, 510, 150,30), " release person1 ")){ //npc.active = true; npc.SetActiveRecursively(true);

npc.transform.parent = null;

}

This is part of a few on gui pages. I parent the npc to the main player so that when I move though different levels, they come with me. Any help is appreciated!

EDIT- I almost forgot, I need to beable to activate npcs that are already set to active = false at the start of the game if it is possible. Please help me out

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 Eric5h5 · Mar 22, 2011 at 01:45 AM 0
Share

$$anonymous$$ark the answer as accepted; there's no need to edit the title.

avatar image superventure · Mar 22, 2011 at 02:12 AM 0
Share

I tried to- it won't let me for some amount of hours yet

2 Replies

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

Answer by superventure · Mar 22, 2011 at 12:53 AM

Sometimes when I ask questions, I kind of hope people would try to think out of the box with me a little bit, but I guess beggars can't be choosers. I HAD to get this to work and stumbled across the answer:

I used SetActiveRecursively(false) to disable the npc, setting this line of code right after the positioning and parenting code lines to attach it in the player.

My problem was getting the npc to COME BACK. I was trying to call it back by directly referring to the disabled npc. SOOO, taking a second look at how 'SetActiveRecursively' works, turns out I was directing what to re activate to the wrong item. The solution- tell setactiverec. to fully activate the PLAYER. It reactivates ALL THE GAMEOBJECTS CHILDREN, including the npc I deactived and attached to it. Since I have multiple level items deactivated in the player as well I dont want setactiverec to re activate them also, so I'm going to have to create an empty game that will follow the player around to store npcs and such.

Hope this helps someone else in the future

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

Answer by DaveA · Mar 22, 2011 at 12:07 AM

When things go inactive (active == false), 'Find' will not find them, so that's why you get the null exception the next time. I think you want 'enable = false' instead, probably on any/all components in the object you need 'off'

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 superventure · Mar 22, 2011 at 12:28 AM 0
Share

That give me an error message (enabled is not a member of unity game object). There has to be some way this can work with acitve true/false?

avatar image DaveA · Mar 22, 2011 at 01:58 AM 0
Share

With game object, you'd have to store the game object variable and set the active on that. With enabled, you'd GetComponent(s)(InChildren) and set/clear 'enabled' on them.

avatar image superventure · Mar 22, 2011 at 06:35 AM 0
Share

by the way, I just found out that 'Find' will find them with this- Npc = transform.Find("Npc").gameObject; Npc.SetActiveRecursively(true);

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

No one has followed this question yet.

Related Questions

Platform moves when character steps on it 1 Answer

Is there any command that can reference deactivated objects? 1 Answer

Coroutine couldn't be started becuase inactive 0 Answers

how to activate/de-activate different scripts on a gameobject? 2 Answers

How to activate the child of an object without declaring a public transform? 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