• 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 jamesstephenbrown · Sep 16, 2016 at 11:36 AM · c#gameobjectposition

Having trouble tracking the position of an object in C#. Position is changing in inspector but not in code.

Hi everyone,

I have a very strange problem I haven't been able to solve for a few hours. I am trying to trigger and event when two objects get close to each other. I've never had any trouble doing this in the past, but for some reason this time, the position of the objects I'm looking at appears not to be updating, and when I print() it returns the original start positions of the objects rather than their current position.

In the game window and the inspector I can see that the objects (Agents) are changing position. But the event is not being triggered because the code thinks the objects are in their original starting positions.

I have some weird natural languages features in my code, like an "a" class and a "the" class, which hold public variables and are present in the scene. The Self class "my" inherits from an Agent class with one variable Self class "my". The Self class is directly on the game object. The only reason I have Agent separate from Self is so that the player can take control of any Self class in the scene, without having to have it's own Self class.

 using UnityEngine;
 using System.Collections;
 using System.Collections.Generic;
 
 public class Brain : Agent {

     public List<Self> sight;
     public float time;
 
     void Start () {
         the = my.the;
         a = my.a;
     }
 
     void Update () {
         // set thinking interval
         if (time <= Time.deltaTime - 0.5) {
             time = Time.deltaTime;
             Think ();
         }
 
         // set a random destination based on current destination
         Vector2 randomVector = Vector2.one * Random.Range(-0.1f, 0.1f);
         my.destination += randomVector;
     }
 
     void Think() {
 
         print(FindObjectOfType<Agent>().transform.position); // a test that always returns the initial starting position rather than the current position (I don't know why)
 
         foreach (Self their in transform.parent.GetComponentsInChildren<Self>()) {
 
             if (their != my) { // don't perform this action on myself
                 
                 print (my.name + "'s position is... " + my.transform.position); // returning start position not current position
                 print (their.name + "'s position is... " + their.transform.position); // returning start position not current position
 
                 if (the.Distance (my.transform.position, their.transform.position) < my.vision && !sight.Contains (their)) {
 
                     Limb limb = (Limb)Instantiate (a.limb);
                     limb.transform.parent = my.transform;
                     limb.transform.localPosition = Vector2.zero;
                     limb.focus = their.gameObject;
                     sight.Add (their);
 
                 }
             }
         }
     }

The limb is instantiating fine at the start of the game, for those objects that begin within the vision distance of each other, but this is supposed to update on an interval throughout the game.

Any help would be much appreciated. For further reference here are the other classes.

 using UnityEngine;
 using System.Collections;

 public class Grammar : MonoBehaviour {
 
     public A a;
     public The the;
 }

 using UnityEngine;
 using System.Collections;
 
 public class A : MonoBehaviour {
 
     public Limb limb;
     public Node node;
     public Link link;
 }

 using UnityEngine;
 using System.Collections;
 
 public class The : MonoBehaviour {
 
     public Grid grid;
 
     public Structure border;
     public Structure map;
 
     public Player player;
 
     public GameObject squareParent;
     public GameObject linkParent;
     public GameObject nodeParent;
 
     public Color red;
     public Color yellow;
     public Color blue;
     public Color orange;
     public Color green;
     public Color purple;
     public Color gray;
     public Color darkGray;
     public Color black;
     public Color white;
 
     public void NewColor(GameObject gameObject, Color color) { gameObject.GetComponent<SpriteRenderer> ().color = color; }
     public Vector2 HeadingFrom(float angle) { return new Vector2(Mathf.Cos(angle * Mathf.Deg2Rad), Mathf.Sin(angle * Mathf.Deg2Rad)); }
     public float AngleFrom(Vector2 heading) { return Mathf.Atan2 (heading.y, heading.x) * Mathf.Rad2Deg; }
     public Vector2 Relative(Vector2 heading, float z) { return (Vector2)(HeadingFrom(AngleFrom(heading) - z)); }
     public float Distance(Vector2 from, Vector2 to) { return (Heading(from,to)).magnitude; }
     public Vector2 Heading (Vector2 from, Vector2 to) { return to - from; }
     public Vector2 MidPoint (GameObject my, GameObject their) { return (my.transform.position + their.transform.position) / 2; }
 }

 using UnityEngine;
 using System.Collections;
 
 public class Agent : Grammar {
 
     public Self my;
 
     void Start () {
     
     }
 
     void Update () {
     
     }
 }
 
 using UnityEngine;
 using System.Collections;
 
 public class Self : Agent {
 
     public SpriteRenderer skin;
     public CircleCollider2D edge;
     public Rigidbody2D body;
     public Brain brain;
 
     public Square square;
 
     public float vision;
     public float speed;
     public float maxSpeed;
 
     public Vector2 restPosition;
     public Vector2 destination;
     public Vector2 gridPos;
 
     void Awake () {
         a = FindObjectOfType<A> ();
         the = FindObjectOfType<The> ();
         my = this;
         destination = transform.position;
     }
 
     void OnMouseDown() {
         the.player.transform.parent = transform;
     }
 }
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

0 Replies

· Add your reply
  • Sort: 

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

If you’re new to Unity Answers, please check our User Guide to help you navigate through our website and refer to our FAQ for more information.

Before posting, make sure to check out our Knowledge Base for commonly asked Unity questions.

Check our Moderator Guidelines if you’re a new moderator and want to work together in an effort to improve Unity Answers and support our users.

Follow this Question

Answers Answers and Comments

4 People are following this question.

avatar image avatar image avatar image avatar image

Related Questions

How to set to game objects's position from 2 different game objects arrays equal to each other? 0 Answers

How to access variables from an object based on location? C# (Pic inside) 1 Answer

How to determine the direction of an object to the player. 1 Answer

Cannot get game object to correctly follow mouse 2 Answers

problem moving a prefab object in script (c#) 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