Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 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 /
  • Help Room /
This question was closed Aug 12, 2017 at 01:06 PM by stevehun03 for the following reason:

The question is answered, right answer was accepted

avatar image
0
Question by stevehun03 · Aug 04, 2017 at 02:09 PM · instantiate prefab

How to make a prefab remember a public gameObject?

I am making a survival game and i have a problem. I made it so the way you pick up items needs to remember a gameobject because the script is on the item itselft, but when i instantiate the object it doesnt remember that gameobject. How do i fix this? (I dont know everything about scripting yet)

script for the object "stick" :

 private bool Collect;
 public GameObject gameobject;
 private Inventory inventory;

 void Start()
 {
     inventory = gameobject.GetComponent<Inventory>();

     Collect = false;
 }

 void Update()
 {
     if (Collect == true)
         if (Input.GetButton("pickup"))
         {
             inventory.sticks += 1;
             Destroy(gameObject);
         }
 }

 void OnTriggerEnter(Collider other)
 {
     if (other.gameObject.name == ("collector"))
     {
         Collect = true;
     }
 }

 void OnTriggerExit(Collider other)
 {
     if (other.gameObject.name == ("collector"))
     {
         Collect = false;
     }
 }



(collector is a dot in the middle of the screen that has a long block collider and is attached to the camera)

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

  • Sort: 
avatar image
0
Best Answer

Answer by Elthen · Aug 04, 2017 at 02:37 PM

If I undersand correctly; You should make that variable private and in start get a reference to it, like so:


    gameobject= GameObject.FindGameObjectsWithTag("YourTag");
Just create a tag inside the inspector and assign it to your object, then replace "YourTag" with the name of the tag you've created.

Comment
Add comment · Show 2 · 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 stevehun03 · Aug 04, 2017 at 02:42 PM 0
Share

That solved it thanks!

avatar image Elthen stevehun03 · Aug 04, 2017 at 05:02 PM 0
Share

awesome, glad I could help; remember to accept to answer!^^

avatar image
0

Answer by ShadyProductions · Aug 04, 2017 at 02:14 PM

instead of Destroy(gameObject); do

 gameObject.enabled = false;

And from the moment you know that you can 100% remove the item then you destroy it

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 stevehun03 · Aug 04, 2017 at 02:20 PM 0
Share

That can be useful but i already dont need it after i have added +1 to the inventory .

avatar image ShadyProductions stevehun03 · Aug 04, 2017 at 02:21 PM 0
Share

Then why do you still need to reference it :) $$anonymous$$aybe I don't understand your question, what do you mean with after instantiate it doesn't remember it. Can you explain? and maybe share the instantation code aswel.

avatar image stevehun03 ShadyProductions · Aug 04, 2017 at 02:23 PM 0
Share

because i want the game to be a survival game and when you cut down a tree it drops sticks and logs

Show more comments

Follow this Question

Answers Answers and Comments

68 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

Related Questions

Only allowing one instance of prefab created by button press & destroying prefab on collision? 0 Answers

How to instantiate a prefab using a string variable ? 1 Answer

How to copy GameObject and preserve Prefab connection and Component values from editor script 2 Answers

Making a time bomb 1 Answer

Problems with instantiated objects in a circle formation 0 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