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 /
avatar image
0
Question by CristiBala24 · Jul 26, 2018 at 09:57 AM · unity 5scripting problemgameobject3dgames

Script on multiple objects not working properly!

So I am making a 3D tower defense game and I have my node script on multiple nodes but when I click on one node to build the tower it builds the tower on the first node I created and it acts as one object.

Here is the script:

using System.Collections; using System.Collections.Generic; using UnityEngine; using UnityEngine.UI;

public class Nodes : MonoBehaviour {

 private Renderer rend;
 private Color startColor;
 private float time = 2.5f;
 private bool textOnScreen;
 private MeshRenderer render;

 public TowerPlacement tower;
 public GameObject node;
 public Text cantbuild;
 public Shop shop;

 void Start ()
 {
     rend = node.GetComponent<Renderer>();
     startColor =rend.material.color;
     textOnScreen = false;
     render = node.GetComponent<MeshRenderer>();
 }

 void Update()
 {
     if (shop.selected == true) render.enabled = true;
     else render.enabled = false;
 }

 void OnMouseDown()
 {
     if(tower.canBuild == false)
     {
         cantbuild.gameObject.SetActive(true);
         if (textOnScreen == false)
         {
             StartCoroutine(cantBuild());
         }
     }
     else
     {
         if (shop.selected == true)
         {
             tower.towerBuilding();
             shop.selected = false;
         }
     }
 }

 void OnMouseEnter()
 {
     if (tower.canBuild) rend.material.color = Color.green;
     else
     {
         rend.material.color = Color.red;
     }
 }

 void OnMouseExit()
 {
     rend.material.color = startColor;   
 }

 private IEnumerator cantBuild()
 {
     textOnScreen = true;
     yield return new WaitForSeconds(time);
     cantbuild.gameObject.SetActive(false);
     textOnScreen = false;
 }

}

Comment
Add comment · Show 7
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 Luo_Yang · Jul 26, 2018 at 10:15 AM 0
Share

Can you show the TowerPlacement script? I guess there's a problem here.

avatar image CristiBala24 Luo_Yang · Jul 26, 2018 at 10:43 AM 0
Share

Ye sure. Here it is :

using System.Collections; using System.Collections.Generic; using UnityEngine;

public class TowerPlacement : $$anonymous$$onoBehaviour {

 public GameObject standardTower;
 public bool canBuild = true;
 public Vector3 positionOffSet;
 public Nodes node;

 public void towerBuilding()
 {
     standardTower = (GameObject)Instantiate(standardTower, node.transform.position + positionOffSet, Quaternion.identity);
     canBuild = false;
 }

}

avatar image Luo_Yang CristiBala24 · Jul 26, 2018 at 10:57 AM 0
Share

I guess you forgot to set up node. Test the code below.The node will be passed in as a parameter.

 public class Nodes : $$anonymous$$onoBehaviour
 {
     private Renderer rend;
     private Color startColor;
     private float time = 2.5f;
     private bool textOnScreen;
     private $$anonymous$$eshRenderer render;
     public TowerPlacement tower;
     public GameObject node;
     public Text cantbuild;
     public Shop shop;
 
     void Start()
     {
         rend = node.GetComponent<Renderer>();
         startColor = rend.material.color;
         textOnScreen = false;
         render = node.GetComponent<$$anonymous$$eshRenderer>();
     }
 
     void Update()
     {
         if (shop.selected == true) render.enabled = true;
         else render.enabled = false;
     }
 
     void On$$anonymous$$ouseDown()
     {
         if (tower.canBuild == false)
         {
             cantbuild.gameObject.SetActive(true);
             if (textOnScreen == false)
             {
                 StartCoroutine(cantBuild());
             }
         }
         else
         {
             if (shop.selected == true)
             {
                 tower.towerBuilding(this);
                 shop.selected = false;
             }
         }
     }
 
     void On$$anonymous$$ouseEnter()
     {
         if (tower.canBuild) rend.material.color = Color.green;
         else
         {
             rend.material.color = Color.red;
         }
     }
 
     void On$$anonymous$$ouseExit()
     {
         rend.material.color = startColor;
     }
 
     private IEnumerator cantBuild()
     {
         textOnScreen = true;
         yield return new WaitForSeconds(time);
         cantbuild.gameObject.SetActive(false);
         textOnScreen = false;
     }
 
     public class TowerPlacement : $$anonymous$$onoBehaviour
     {
         public GameObject standardTower;
         public bool canBuild = true;
         public Vector3 positionOffSet;
 
         public void towerBuilding(Nodes pNode)
         {
             standardTower = (GameObject) Instantiate(standardTower, pNode.transform.position + positionOffSet,
                 Quaternion.identity);
             canBuild = false;
         }
     }
Show more comments
avatar image CristiBala24 Luo_Yang · Jul 26, 2018 at 10:46 AM 0
Share

Also here is a video of my problem : https://www.youtube.com/watch?v=B1hjux$$anonymous$$qsRw

Thank you for your help!

avatar image Luo_Yang CristiBala24 · Jul 26, 2018 at 11:34 AM 0
Share

Before I saw the video, I mistaken your question. The real reason is that all the nodes are building when the mouse is pressed. Therefore, a judgement is added to build the operation only at the node where the mouse enters.

 public class Nodes : $$anonymous$$onoBehaviour
 {
     private Renderer rend;
     private Color startColor;
     private float time = 2.5f;
     private bool textOnScreen;
     private $$anonymous$$eshRenderer render;
     public TowerPlacement tower;
     public GameObject node;
     public Text cantbuild;
     public Shop shop;
 
     void Start()
     {
         rend = node.GetComponent<Renderer>();
         startColor = rend.material.color;
         textOnScreen = false;
         render = node.GetComponent<$$anonymous$$eshRenderer>();
     }
 
     void Update()
     {
         if (shop.selected == true) render.enabled = true;
         else render.enabled = false;
     }
 
     private bool _$$anonymous$$ouseEnter = false;
 
     void On$$anonymous$$ouseDown()
     {
         if (tower.canBuild == false)
         {
             cantbuild.gameObject.SetActive(true);
             if (textOnScreen == false)
             {
                 StartCoroutine(cantBuild());
             }
         }
         else
         {
             if (shop.selected == true && _$$anonymous$$ouseEnter)
             {
                 tower.towerBuilding(this);
                 shop.selected = false;
             }
         }
     }
 
     void On$$anonymous$$ouseEnter()
     {
         if (tower.canBuild) rend.material.color = Color.green;
         else
         {
             rend.material.color = Color.red;
         }
 
         _$$anonymous$$ouseEnter = true;
     }
 
     void On$$anonymous$$ouseExit()
     {
         rend.material.color = startColor;
         _$$anonymous$$ouseEnter = false;
     }
 
     private IEnumerator cantBuild()
     {
         textOnScreen = true;
         yield return new WaitForSeconds(time);
         cantbuild.gameObject.SetActive(false);
         textOnScreen = false;
     }
 }
 
 public class TowerPlacement : $$anonymous$$onoBehaviour
 {
     public GameObject standardTower;
     public bool canBuild = true;
     public Vector3 positionOffSet;
 
     public void towerBuilding(Nodes pNode)
     {
         standardTower = (GameObject) Instantiate(standardTower, pNode.transform.position + positionOffSet,
             Quaternion.identity);
         canBuild = false;
     }
 }
Show more comments

1 Reply

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

Answer by madks13 · Jul 26, 2018 at 03:15 PM

From the comments and the video, i'd say you use the wrong renderer. have you tried placing the first tower, which should appear at the wrong place, then trying to place a tower at another placement, a 3rd one? If you can, then the towers are properly instanciated, but you have the same node reference. Otherwise something in your tower creation is wrong.

Edit : i thought this was addressed in the comments, but where do you set up TowerPlacement tower variable? I see it used, but it's not set anywhere.

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 CristiBala24 · Jul 26, 2018 at 03:25 PM 0
Share

I resolved the part with the tower building but now i have another problem. After i place one tower i cant build on the other nodes cause i have a boolean that verifies if i have something on a node. The problem is that the boolean changes for all nodes not just the one that has a tower on it. Thank you for your comment

avatar image madks13 CristiBala24 · Jul 27, 2018 at 09:32 AM 0
Share

What i have understood is that you have only one instance of TowerPlacement. You should either add a TowerPlacement component to each node in the editor or via code like this :

         void Start()
         {
             rend = node.GetComponent<Renderer>();
             startColor = rend.material.color;
             textOnScreen = false;
             render = node.GetComponent<$$anonymous$$eshRenderer>();
            //New line, add a TowerPlacement component to the node
             tower = node.AddComponent<TowerPlacement>(); 
         }

Sine you use this component to check if you can build, if you only have one instance of it, it's obvious it will return false after only having built one tower. From the way you made your code, i think you should make TowerPlacement a static class and have all the logic of selecting the right mesh, checking if tower can be built on node, etc inside it.

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

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

Related Questions

How can i look for collisions of the bulidngs in may array ? how can i use OnTRiggerEnter/exit ? 0 Answers

how to set an object on a path,how to set an object on a track 0 Answers

Refresh panel with prefab contained value from json that created using array 0 Answers

cannot drag script to player.Guitext error,cannot drag player script to the player in hierarchy 2 Answers

Is there a way to dynamically attach a script to a GameObject during runtime? 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