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 MrJCSmith · Sep 09, 2014 at 02:52 AM · c#instantiateprefab

All instances of a an object are being updated at once instead of the object in question.

Hi all,

First time posting a question and I'm new to unity so bear with me.

I've run into a problem with one of my base building scripts. The way it is supposed to work is like this; the player walks onto the buildsite and selects a building to construct. Currently by either pressing '1' or '2'. Then once that is complete the option to upgrade becomes available. The snag at the moment though is that if the player has enough resources then all instances of that building are being updated at once.

What I actually want is that only that one buildsite is updated.

The build code goes like this:

 `using UnityEngine;
 using System.Collections;
 
 public class constructionScript : MonoBehaviour {
         
 
         private resourceGUI material;
         private bool canBuild = false;
         public Object Building;
         public Object Building2;
         
         bool toggleGUI;
 
         void  Start (){
         material = GameObject.Find("Player").GetComponent<resourceGUI>();
         }
         
         void  OnTriggerEnter (Collider other){
             toggleGUI = true;
             canBuild = true;
         }
         
         void  OnTriggerExit ( Collider other){
             toggleGUI = false;
         }
         
         void  OnGUI (){
             if (toggleGUI == true)
                 GUI.Label (new Rect(500, 300, 300, 40), "Press '1' to build Barracks.\nPress '2' to build Forge.");
                 
         }
 
         void  Update (){
         if(canBuild == true)
         {
             if(Input.GetKeyDown("1"))
             {
                 if(material.currentWood >= 1 && material.currentMetal >= 1 && material.currentStone >= 1)
                 {
                     Instantiate(Building,transform.position,transform.rotation);
 
                     material.currentWood--;
                     material.currentMetal--;
                     material.currentStone--;
 
                     Destroy(gameObject);
 
                 }
             }
 
             if(Input.GetKeyDown("2"))
             {
                 if(material.currentWood >= 1 && material.currentMetal >= 1 && material.currentStone >= 1)
                 {
                     Instantiate(Building2,transform.position,transform.rotation);
 
                 //Set materials to zero
                 material.currentWood--;
                 material.currentMetal--;
                 material.currentStone--;
                 
                 Destroy(gameObject);
                 }
             }
         }
 
     }
 }

At the moment this allows me to build individual buildings on each buildsite even if I have more than enough resources to update all the buildsites at once. The problem seems to be in my upgrade script.

This is the script that is attached to the prefabs that are called in the above script:

 using UnityEngine;
 using System.Collections;
 
 public class upgradeScript : MonoBehaviour {
     
     
     private resourceGUI material;
     private constructionScript Building;
     private bool canBuild = false;
     public Object buildingUpgrade;
     
     bool toggleGUI;
     
     void  Start (){
         material = GameObject.Find("Player").GetComponent<resourceGUI>();
     }
     
     void  OnTriggerEnter (Collider other){
         toggleGUI = true;
         canBuild = true;
     }
     
     void  OnTriggerExit ( Collider other){
         toggleGUI = false;
     }
     
     void  OnGUI (){
         if (toggleGUI == true)
             GUI.Label (new Rect(500, 300, 300, 40), "Press 'U' to upgrade building.");
         
     }
     
     void  Update (){
         if(canBuild == true)
         {
             if(Input.GetKeyDown("u"))
             {
                 if(material.currentWood >= 2 && material.currentMetal >= 2 && material.currentStone >= 2)
                 {
                     Instantiate(buildingUpgrade,transform.position,transform.rotation);
 
                     material.currentWood-=2;
                     material.currentMetal-=2;
                     material.currentStone-=2;
 
                     Destroy(transform.parent.gameObject);
                 }
             }
 
         }
         
     }
 }


Could the problem be that the scripts are attached to prefabs and are all clones?

Any help on this is greatly appreciated as is any criticism about the code in general. I've next to no knowledge of both unity and C# but I have gone through a few of the tutorials and played around with some of the scripts that can be found here and there so this is all a learning experience to me.

Cheers!

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

1 Reply

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

Answer by AyAMrau · Sep 09, 2014 at 03:13 AM

You never set the canBuild variable to false, so if the player touched the building once it will stay selected for upgrade.

In OnTriggerExit add canBuild = false;

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 MrJCSmith · Sep 09, 2014 at 08:08 AM 0
Share

Awesome, thank you for that! It works as intended now.

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

3 People are following this question.

avatar image avatar image avatar image

Related Questions

Spawning a prefab at another object's location 3 Answers

Issue Instantiating prefab in C# 0 Answers

How do I make a clone of a prefab appear on the correct layer? [5.2.2f1] 1 Answer

Prefabs instantiated from an array are keeping their public int value 1 Answer

How to break a prefab connection via c# 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