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 zpunehx · May 19, 2014 at 07:57 AM · c#coroutinertsbuildingsconstruction

constructing buildings rts style.

currently i have a co routine that starts after i lock the position of the building prefab. this coroutine is attached to my builder unit which simulates or "fakes" constructing a building. i have a delegate that i pass to my gui system ; which on button click does the entire "action" of construction.

problem:

the coroutine keeps starting over and over again for some reason. (Final Placement Method).

Construction Script:

public class Construction : MonoBehaviour {

 public GameObject [] buildings;
 public Action.myActions constructingBarr;
 public Action.myActions constructingBase;
 public Color MatColor {
     get {
         return materialColour;
     }
     set {
         myRenderer.material.color 
         = value;
     }
 }
 public bool IsPlaced {
     get {
         return isPlaced;
     }
     set {
         isPlaced = value;
         if(isPlaced!=false) {
             StopCoroutine ("ConstructBuilding");
             StartCoroutine ("ConstructBuilding");
         }
     }
 }

 private bool isPlaced;
 private Builder myBuilder;
 private Color materialColour;
 private Renderer myRenderer;
 private MeshRenderer myMesh;
 private Transform currentBuilding;

 private void Start() {
     myBuilder = GetComponent <Builder> ();
     myMesh = GetComponentInChildren <MeshRenderer> ();
 }

 private void FetchRenderer() {
     try {
         myRenderer = currentBuilding.
             GetComponentInChildren<Renderer> ();
     }catch {};
 }

 private void Update() {
     LocatingBuildingPos ();
     Debug.Log (isPlaced);
 }

 private void SaveOrginalMatColour () {
     try{
         materialColour = 
             myRenderer.material.color;
     }catch {};
 }

 public bool InstantiateBuilding(GameObject building) {
     currentBuilding = ((GameObject)Instantiate
                        (building)).transform;
     FetchRenderer ();
     return IsPlaced = !true; 
 }

 public void CreateBase() {
     InstantiateBuilding (buildings [1]);
     SaveOrginalMatColour ();
 }

 public void CreateBarracks() {
     InstantiateBuilding (buildings [0]);
     SaveOrginalMatColour ();
 }

 public void LocatingBuildingPos() {
     if (!IsPlaced) {
         try {
             var cameraYPos = Camera.main.transform.
                 position.y;
             var mousePos = new Vector3
                 (Input.mousePosition.x,
                  Input.mousePosition.y,
                               cameraYPos);
             var mousePosWorld = 
             Camera.main.ScreenToWorldPoint (mousePos);
             currentBuilding.transform.position 
                 = new Vector3 (mousePosWorld.x, 0, 
                                mousePosWorld.z);
         }
         catch {};
     }
     FinalPlacement ();
 }

 IEnumerator ConstructBuilding () {
     Vector3 previousBuilderPos = transform.position;
     Vector3 buildingPos = currentBuilding.
             transform.position;
     SetConstructionTarget (buildingPos);
     yield return new WaitForSeconds (4f);
     IsNotConstructing (false);
     MatColor = Color.red;
     yield return new WaitForSeconds (4f);
     MatColor = Color.yellow;
     yield return new WaitForSeconds (8f);
     MatColor = Color.green;
     myBuilder.Target = previousBuilderPos;
     yield return new WaitForSeconds (3f);
     myRenderer.material.color = MatColor;
     IsNotConstructing (true);
     Debug.Log ("Construction Complete !");
 }

 public void IsNotConstructing(bool value) {
     myMesh.enabled = value;
     gameObject.collider.enabled = value;
     currentBuilding.collider.enabled = value;
 }

 public void SetConstructionTarget(Vector3 target) {
     myBuilder.Target = target;
 }

 public bool FinalPlacement () {
     if (Input.GetMouseButtonDown (0)) {
         return IsPlaced = !false;
     } else {
         return !IsPlaced;
     }
 }

 private void OnEnable() {
     constructingBarr += CreateBarracks;
     constructingBase += CreateBase;
     //constructingBarr += myEvent(buildings[1]);
     //constructingBase += myEvent(buildings[0]);
 }

 private void OnDisable() {
     constructingBarr -= CreateBarracks;
     constructingBase -= CreateBase;
     //constructingBarr -= myEvent(buildings[1]);
     //constructingBase -= myEvent(buildings[0]);
 }

}

Comment
Add comment · Show 2
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 Kiwasi · May 19, 2014 at 07:52 PM 0
Share

BTW we really didn't need the entire class to solve this problem. The following would have been sufficient and got you better and quicker feedback

 private bool isPlaced;
  
 private void Update() {
     LocatingBuildingPos ();
     Debug.Log (isPlaced);
 }
   
 public void LocatingBuildingPos() {
     if (!IsPlaced) {
         // Do some stuff
     }
     FinalPlacement ();
 }
   
 public bool FinalPlacement () {
     if (Input.Get$$anonymous$$ouseButtonDown (0)) {
        return IsPlaced = !false;
     } else {
        return !IsPlaced;
     }
 }

avatar image zpunehx · May 20, 2014 at 05:18 AM 0
Share

thank you. do u $$anonymous$$d checking my other question ? http://answers.unity3d.com/questions/710815/setting-a-target-for-projectile.html

1 Reply

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

Answer by Em3rgency · May 19, 2014 at 12:38 PM

LocatingBuildingPos() is called every frame by Update(). FinalPlacement() is called every time LocatingBuildingPos() is called, because its outside the if statement.

So... FinalPlacement() is called every frame. You wrote your code that way.

Comment
Add comment · Show 3 · 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 zpunehx · May 19, 2014 at 05:48 PM 0
Share

Final placement locks the position of the building. where should i put it to avoid the calling everyframe

avatar image Kiwasi · May 19, 2014 at 07:44 PM 0
Share

$$anonymous$$ost of the time you put a function inside an if with a bool to indicate if you want to call it.

 // Set this to true when the conditions are satisfied to call final placement
 private bool IWantToCallFinalPlacement;
 
 if(IWantToCallFinalPlacement){
     FinalPlacement();
     IWantToCallFinalPlacement = false;
 }

Also your return code for FinalPlacement() is weird.

avatar image zpunehx · May 19, 2014 at 08:10 PM 0
Share

basically what i am want to do is this:

-if i press right click -lock the position of the building where the mouse is and start construction.

is there another way besides that flag?

also can you take a look at my other question?

http://answers.unity3d.com/questions/710815/setting-a-target-for-projectile.html

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

22 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

Related Questions

Multiple Cars not working 1 Answer

Distribute terrain in zones 3 Answers

Displaying buildable area as an overlay on terrain 0 Answers

Lerp in Coroutine (Crazy Behavior) 2 Answers

C# WaitForSeconds isn't waiting for any seconds 2 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