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 miccalisto · Jul 24, 2020 at 07:30 PM · referencingreference-other-object

Reference instantiated object

I'm new to Unity, but have this strange problem. When I instantiate an object in which I have an update method that instantiate another object and this object has a script that increases value of variable of the first instaniated object it strangely increases the value of the variable in the assets menu not in the actual game.

And even strangely the increased value in the assets menu stays increased even after I stop the game.

New instance of a player:

     void SetupPlayer()
         {
             GameObject newPlayer = Instantiate(player);
             newPlayer.GetComponent<Player>().food = 100;
             newPlayer.GetComponent<Player>().wood = 200;
             newPlayer.GetComponent<Player>().ores = 50;
             newPlayer.GetComponent<Player>().manpower = 10000;
         }

This script is attached to the player:

 private void Update()
     {
         if (Input.GetMouseButtonDown(0))
         {
             Instantiate(temp);
             print(foodIncome);
         }
     }

And this is attached to the "temp" object:

     int income = 100;
     public GameObject player;
 
     private void Awake()
     {
         player.GetComponent<Player>().foodIncome += income;
     }

I don't understand what's going on.

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
1

Answer by unity_ek98vnTRplGj8Q · Jul 24, 2020 at 07:56 PM

Your "player" variable on the script of you temp object is still pointing to the prefab, not to the instantiated player object. Do this when you spawn it instead

 if (Input.GetMouseButtonDown(0))
          {
              Gameobject newTemp = Instantiate(temp);
              newTemp.GetComponent<WhateverTheTempScriptIsCalled>().player = this.gameObject;
              print(foodIncome);
          }
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 miccalisto · Jul 24, 2020 at 08:15 PM

I did exactly this and I got an error that the player variable has not been assigned. Even though when I checked the instantiated "temp" objects in the inspector it all had "player(clone)" assigned to it, so it should work, but for some reason it doesn't register it. @unity_ek98vnTRplGj8Q

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 unity_ek98vnTRplGj8Q · Jul 24, 2020 at 08:28 PM 0
Share

Change Awake() to Start() in your temp script, awake is called with the instantiate call before you have assigned the player variable, but Start is called sometime later so it will work there

avatar image miccalisto unity_ek98vnTRplGj8Q · Jul 24, 2020 at 08:34 PM 0
Share

thank you very much, now it works perfectly fine

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

131 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 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

how do i refer to a instantiated gameobject? 1 Answer

Referencing the Collision of two other objects to allow for an animation to be played on the scripted object. 2 Answers

Script is losing references once gameobjects get disabled. How can I fix this? 0 Answers

Getting a " NullReferenceException: Object reference not set to an instance of an object " - Space Shooter Tutorial 14 - Counting Points 3 Answers

What is the best method for handling references to objects on Clients from a Network.Spawn? 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