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 HsoulT · May 19, 2013 at 04:38 AM · gameobjectbugvariablemonodevelop

Why isn't the variable being assigned?

I'm using the unity debugger to find out why a variable isn't being changed. What I found was very strange. alt text

The yellow mark is where the code execution currently is. So the variable hold should be assigned. The position 8,2,2 in map indeed holds a character object according to the debugger. However, hold which is assigned to map[8,2,2] does not have a reference to a character object, instead it has one to a null obj.

So my question is, why isn't the variable being assigned properly?

Edit:

alt text

alt text

You can see that there is an object at map[8,2,2] already. The problem is that hold isn't being assigned to the value at 8,2,2. Instead it is being assigned to a different Entity, which holds a reference to a null object.

Edit #2 (added code for Entity class)

 using UnityEngine;
 using System.Collections;
 using Newtonsoft.Json;
 
 [JsonObject(MemberSerialization.OptIn)]
 public class Entity : System.IEquatable<Entity>
 {
     [JsonProperty]
     public int x;
     [JsonProperty]
     public int y;
     [JsonProperty]
     public int z;
     [JsonProperty]
     public states type;
     
     public GameObject obj;
     
     public Entity()
     {
         obj = null;
         this.type=states.empty;
     }
     
     public Entity(GameObject reference, states type)
     {
         obj = reference;
         x = (int) obj.transform.position.x;
         y = (int) obj.transform.position.y;
         z = (int) obj.transform.position.z;
         this.type = type;
     }
     
     public bool Equals(Entity other)
     {
         return this.x==other.x&&this.y==other.y
             &&this.z==other.z&&this.type==other.type;
     }
             
     public string getCoordinatesasString()
     {
         return x.ToString()+','+y.ToString()+','+z.ToString()+','+type.ToString();
     }    
         
     public Vector3 getCoordinates()
     {
         return new Vector3(x,y,z);
     }
     
     public void setData(int x, int y, int z, states type)
     {
         this.x=x;
         this.y=y;
         this.z=z;
         this.type=type;
     }
     
 }
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
1
Best Answer

Answer by Tomer-Barkan · May 19, 2013 at 06:47 AM

Edit:

Oh! I don't know how I missed this earlier... You can't cast a float to an int using (int)someFloat... it won't work!

You can use Mathf.RoundToInt(someFloat) instead. Try it.

So change the assignment line to

 Entity hold = map[Mathf.RoundToInt(position.x), Mathf.RoundToInt(position.y), Mathf.RoundToInt(position.z)];
Comment
Add comment · Show 8 · 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 HsoulT · May 19, 2013 at 03:59 PM 0
Share

The map isn't empty though, I attached some more screenshots to show this.

avatar image HsoulT · May 19, 2013 at 04:08 PM 0
Share

I should mention the default constructor for an Entity assigns a null obj

avatar image HsoulT · May 19, 2013 at 05:21 PM 0
Share

Added the code

avatar image Tomer-Barkan · May 19, 2013 at 07:39 PM 0
Share

Oh! I don't know how I missed this earlier... don't cast a float to an int using (int)someFloat... it doesn't always yield the result you expect!

You can use $$anonymous$$athf.RoundToInt(someFloat) ins$$anonymous$$d. Try it.

avatar image HsoulT · May 19, 2013 at 08:29 PM 0
Share

I used RoundToInt ins$$anonymous$$d, and that fixed the problem! Though could you explain why it did? Shouldn't (int) cast it to an int?

Show more comments

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

14 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

Related Questions

how can I display a variable as a GUIText 5 Answers

Trivial question for Javascript/Unityscript experts: static variables? 4 Answers

Assigning current color to a variable for fade out (C#) 0 Answers

Cant Add Script (or create any new scripts) Javascript 2 Answers

How to create gameobject with different setting 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