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 edwincheng120402 · Jun 16, 2020 at 07:39 AM · bug-perhaps

Why would a Vector2 reset its original state after I assigned a value to it?

 using System;
 using System.Collections;
 using System.Collections.Generic;
 using System.Linq;
 using UnityEngine;
 using UnityEngine.UI;
 
 public class Building : MonoBehaviour
 {
     [SerializeField] public int owner;
     [SerializeField] public int tpa, time, level;
     [SerializeField] public double maxHP, HP, atk, def, range, prod, cost;
     [SerializeField] private Vector2 coor;
     [SerializeField] public string type, branch;
 
     public static double totalprod;
     public static int actions;
 
     [SerializeField] bool buildStarted = false;
     [SerializeField] bool buildCompleted = false;
     int buildStartedOn;
     bool complete = false;
     
 
     public static List<Building> myBuildings = new List<Building>();
 
     public Vector2 Coor { get => coor;}
 
     public Building(double hp, int turn_per_action, double attack, double defence, double r, double pro, double c, int t, string typ, int lvl, string bra, int own, Vector2 co)//constructor
     {
         owner = own;
         HP = hp;
         tpa = turn_per_action;
         atk = attack;
         def = defence;
         range = r;
         prod = pro;
         cost = c;
         time = t;
         type = typ;
         level = lvl;
         branch = bra;
         coor = co;
 
 
     }
     private void Awake()
     {
         //Building building = new Building(HP, tpa, atk, def, range, prod, cost, time, type, level, branch, owner, coor);
         if(owner == 0)
         {
             myBuildings.Add(this);
         }
 
 
     }
     private void Update()
     {
         if(owner == 0)
         {
 
             if (buildCompleted)
             {
 
                 if (complete == false)
                 {
                     totalprod += prod;
                     if (type == "Base")
                     {
                         //tpa of base is apt
                         actions += tpa;
                     }
                     Debug.Log(coor);
                     complete = true;
                 }
 
             }
             else if (buildStarted == false)
             {
                 if (Currency.resource >= cost)
                 {
                     Currency.resource -= cost;
                     BuildDisplay.buildAction--;
                     Vector2 coor = NewBuilding.co;
                     Debug.Log(coor);
 
                     buildStarted = true;
                     buildStartedOn = Turn.turnCourt;
                 }
                 else
                 {
                     MenuManger.Messaging("no resource");
                     Destroy(gameObject);
                 }
             }
         }
 
         if(buildCompleted == false)
         {
             if(Turn.DeltaT(buildStartedOn) >= time)
             {
                 buildCompleted = true;
             }
         }
 
         if(HP <= 0)
         {
             Destroy(gameObject);
         }
 
     }
 
     public void OnBuilding()
     {
         if (buildCompleted)
         {
             if (SelectAction.isTargeting)
             {
                 if (owner != 0)
                 {
                     Attacking.TargetBuilding = this;
                     Attacking.isAttacking = true;
 
                 }
                 else
                 {
                     MenuManger.Messaging("invalid target");
                 }
             }
             else if (owner == 0)
             {
                 SelectAction.ActingBuilding = this;
                 MenuManger.ChangeMenu("Action", true);
             }
         }
         else
         {
             MenuManger.Messaging("incomplete building");
         }
     }
     public static List<Building> FindBuildingWithType(string tType)
     {
         var buildings = myBuildings.Where(x => x.type == tType).ToList();
 
         return buildings;
 
     }
 
 
 }

coor is always 0,0 when I look at it from the inspector. The only time it is correct is in the Debug.Log() right after I changed it. That bit of code runs only once. I made coor private. I used crtl+F to find that this is the only time I changed the value of coor.

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

Answer by unity_ek98vnTRplGj8Q · Jun 16, 2020 at 03:12 PM

You redeclared your coor variable when you assigned it a value -> Vector2 coor = NewBuilding.co;

This means that you now have 2 variables named coor, and the compiler uses the one you just declared rather than the private variable, leaving your private variable untouched. To fix it simply change this line of code to coor = NewBuilding.co;

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

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

127 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

Related Questions

New blank project with 3 leaked textures ??? 1 Answer

Problem with lights. Why is this happening? 0 Answers

Creating gameobjects using script and assigning sprites to some of them breakes batching (both static and dynamic). Bug? 1 Answer

Input.GetAxis("Mouse ScrollWheel") not working without wheel being clicked in 2 Answers

Issue where if I click a button then press W,A,S,D the value of my sliders go down by 10 or up by 10 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