• 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.

Welcome to Unity Answers

The best place to ask and answer questions about development with Unity.

To help users navigate the site we have posted a site navigation guide.

If you are a new user to Unity Answers, check out our FAQ for more information.

Make sure to check out our Knowledge Base for commonly asked Unity questions.

If you are a moderator, see our Moderator Guidelines page.

We are making improvements to UA, see the list of changes.



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

get the gameobject's variable 1 Answer

Variable value not changhing 3 Answers

MonoDevelop Strange Behaviour 3 Answers

How do I detect if a game object exist? 4 Answers

How can I change a variable in a different game object's script? 2 Answers

  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges