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
0
Question by Murkas · Jul 24, 2013 at 12:14 AM · disappearmultiple objects

How to keep objects disabled after reloading the scene?

I've just started kind of a first-person shooter and got the following problem: I want to add magazines to the scene. When the Player look at them and press "E", the object should disappear. I already got that. The problem now is, if I leave the room and enter another, then re-enter the first one, I want that the already grabbed magazines don't appear anymore.

At the moment the script attached to the magazine-mesh looks like this:

 using UnityEngine;
 using System.Collections;
 
 public class Ammo_Script : MonoBehaviour {
 
     bool grabbed=false;
     
     void OnStart(){
         if(grabbed){
             gameObject.SetActive(false);
         }
     }
     
     public void Grab(){
         gameObject.SetActive(false);
         grabbed=true;
         Debug.Log("grabbed ammo");
     }
 }


It is accessed by this script attached to the main camera:

 [...]
                 if(Physics.Raycast(gameObject.transform.position,gameObject.transform.forward,out hitinfo,(float)1.5)){
                     if(hitinfo.transform.name=="Gun_Ammo"){
                         
                         //Ammo_Script Ammo = (Ammo_Script) hitinfo.transform.gameObject.GetComponent("Ammo_Script");
                         //Ammo.Grab();
                         (hitinfo.collider.gameObject.GetComponent("Ammo_Script") as Ammo_Script).Grab();
                         Debug.Log(hitinfo.transform.gameObject.GetInstanceID().ToString());
                         Variables.gun_ammo_count++;
                     }
                 }
 [...]

The Ammo rightly disappears, but if I re-enter the scene, it is shown again. Anyone know how to fix the script? The Ammo-Script exists multiple times.

Comment
Add comment
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

2 Replies

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

Answer by Murkas · Jul 25, 2013 at 02:33 PM

Okay, the following script is working for me:

 using UnityEngine;
 using System.Collections;
 
 public class Ammo_Script : MonoBehaviour {
 
     int own_level;
     
     void Awake() {
         DontDestroyOnLoad(this);
         Debug.Log("ammo-script: loaded level: "+Application.loadedLevel.ToString ()+" reloaded?: "+Variables.entered_rooms[Application.loadedLevel].ToString());
         if(Variables.entered_rooms[Application.loadedLevel]){
             Destroy(gameObject);
         }else{
             own_level=Application.loadedLevel;
         }
     }
 
     void OnLevelWasLoaded(int level){
         if(level==own_level){
             gameObject.collider.enabled=true;
             gameObject.renderer.enabled=true;
             gameObject.rigidbody.freezeRotation=false;
             gameObject.rigidbody.useGravity=true;
         }else{
             gameObject.collider.enabled=false;
             gameObject.renderer.enabled=false;
             gameObject.rigidbody.freezeRotation=true;
             gameObject.rigidbody.useGravity=false;
         }
     }
     
     public void Grab(){
         Destroy(gameObject);
         Debug.Log("grabbed ammo");
     }
 }
 
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
1

Answer by dorpeleg · Jul 25, 2013 at 06:06 AM

The only way you can do that, is if you make some sort of "pickup manager" that will have DontDestroyOnLoad.

The "pickup manager" will remain between scenes and will auto hide all the picked up items.

Comment
Add comment · Show 5 · 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 clunk47 · Jul 25, 2013 at 06:21 AM 0
Share

I feel like an idiot. I've even done this before, just couldn't surface it in my brain lol +1

avatar image Murkas · Jul 25, 2013 at 11:37 AM 0
Share

That sounds good. But I think I don't come around na$$anonymous$$g the objects "ammo_001", "ammo_002", ... ?

avatar image dorpeleg · Jul 25, 2013 at 12:46 PM 0
Share

can I ask why you are switching scenes anyway?

It's not always needed....

But regardless, how are you placing your ammo boxes? by hand from the editor or by code?

avatar image Murkas · Jul 25, 2013 at 01:44 PM 0
Share

I am switching scenes, cause I don't want one big scene. I want to split it in smaller scenes. I am placing my Ammo Boxes by editor.

avatar image dorpeleg · Jul 25, 2013 at 01:49 PM 0
Share

You can try placing DontDestroyOnLoad on the ammo script.

This will make the ammo stay between scenes.

You will have all ammo boxes in all scenes, but if you disable them, they should not bother you.

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

17 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

Related Questions

Putting Together Multiple Rigged Models 1 Answer

Why is my object disappearing at runtime? 6 Answers

Making an image suddenly flash onto screen and then off again? new code (edit) 2 Answers

i am having a problem in using ex2d animation sprite.. 0 Answers

How do i get rid of my Crosshair when i aim? 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