So I create a bunch of Sphere gameobjects through a for loop in code and I attach a drag script to these spheres along with adding the spheres to an array to manage them. Now, when I drag the sphere, the change is not updated. I expect this is because the object is copied so I’m wondering how I should go about this.
I haven’t programmed like this in a while, mostly been used to pointers so something like this would be easy and I’m really tempted to go unsafe and start throwing around pointers like no tomorrow, just because it makes everything easier.
Can you please explain that a bit more? Sorry if I’m being dense but I’m just not getting it… Where and how do you want things to be “updated”? Where and how are you determining that they’re not being updated?
Okay, well, I have a script attached to an empty object I manually create in unity. I attach this script to the object.
What the script does is it initialized a bunch of things in a random location. It stores these newly created spheres in an array for ease purposes. Now, for each new sphere game object, I add a drag controller component (in the code of course) as such:
sphere.AddComponent (“DragController”);
I can drag the sphere just fine but in the array I created, the objects don’t have the updated positions from moving them and I use this array to access the spheres whenever I need to. I mean, normally in C, I’d store the pointer in the array and not the object itself, so if I changed that object somewhere else it’d be changed in the array and all other locations as well.
So, I got it working but it’s sort of a hack. Instead of adding the drag controller to handle moving, I just handle it in my main script and of course I’d rather not but for a small project it’ll do. I’m just curious how I’d go about a problem like this in the future though?
So you capture the result returned from Instantiate, you store that in the array and when querying .transform.position you don’t get the current world position of the object being dragged? If so then I’d rate that as a bug. What you get back from Instantiate is a reference to the newly created game object and you should certainly get its current and updated position.
Perhaps you can share some code and/or a small example project?
for (int i = 0; i<size; i++)
{
Vector3 v = new Vector3 (Random.value, Random.value, 0);
GameObject s = GameObject.CreatePrimitive (PrimitiveType.Sphere);
Vector3 v1 = new Vector3 (35, 35, 35);
s.transform.localScale = v1;
s.transform.position = v;
s.AddComponent ("DragController");
if (i == srcNode || i == dstNode)
{
s.renderer.material.color = Color.red;
}
else
{
s.renderer.material.color = Color.blue;
}
Shader shader1 = Shader.Find ("Specular");
s.renderer.material.shader = shader1;
myNodes.Add (s);
}
So basically a bunch of spheres are created and I add them to myNodes array.
The drag controller is essentially a copy from the drag controller in the RagDoll Lerpz demo. If the mouse click is contained within the the transform object, then drag it. And of course this works fine.
Now back to the class containing myNodes array. Just for testing purposes I check each node in the array against a certain position so if it’s X is greater then this position then do something. It never goes into this if and through extensive testing I saw that the nodes in the myNodes array aren’t being updated with their positions.
When you say that, are you dumping out myNodes[n].transform.position via Debug.Log() or to an in-view GUI element or ? Can you post a sample project that’s ready to go? This is all quite surprising as the code above looks solid (except that you might need to add a rigidbody component to each sphere as I thought the drag script required one).
Well, in the main script it was something as simple as:
if (node.transform.position.x > 800)
{
doSomething();
}
I then ran the scene, dragged my node past 800 and doSomething was not run. If I remove the drag controller, and just handle dragging within the main script, then doSomething IS invoked. I can try to put together a sample project but not today (so sleepy I’m about to pass out lol), but to help you guys with this potential issue I will soon.
I mean, I am very new to Unity it could be some stupid mistake but I’ve had enough experience with this type of thing and to me it seems that my object reference in the drag controller is not the same one as in the myNodes array. Judging by what I’ve read, it SHOULD be the object reference…
Can you do the extra step of due diligence and output the position of the object being dragged somehow? I don’t mean to distrust your sensibilities but if your doSomething function isn’t getting called then let’s be certain that the position values you think it has match what it actually does report back.
Honestly, my gut says that this is a problem with your project and thus the request. If this was some sort of a general issue with Unity the forums would be ablaze with complaints about it. So my gut asks “what’s up with that project?”. Perhaps it is a bug, taking a peek would help sort that out so if/when you can post a project I’m happy to take a look!
Agreed. Something is fishy in here, you talk like you know what you’re doing, your code looks solid and so it’s leaving me a bit puzzled too… We’ll sort it out though, one way or another. Get some sleep and circle back on this when you’ve rested a bit.
Just did it (woke up after a nice 12 hour sleep ), and the position in Debug.Log that I’m outputting is NOT changed. So now instead of doSomething, like you asked I simply output the x and y position of the node, this is all in my main script of course.
However, it’s clearly being dragged and taken past 800. I’ve extensively looked at the code and there’s nothing that suggests I’ve copied the object (I’m sure it’s still the reference). I’ve read tonnes of documentation as well and the more I look into it, the more I’m now certain it’s something with my project as a whole, something I’m not doing because of my n00b status with Unity.
I’m REALLY sorry I can’t put together a sample project today (got a deadline) but I’ll DEFINITELY do it on Monday. I’m very curious as to what I was doing wrong, or who knows maybe I’ve discovered some hidden one in a million bugs (I hate these!). Though I’m probably just using things the way they are not meant to be used.
I mean, I sort of just jumped into this. Had to learn Unity, learn C#, and put together a sample project in 10 days to demonstrate various algorithms in Graph Theory. So I really rushed it and skipped a lot of pre-reading I usually do and just did things with the gut, and some example code. Thanks for your patience though I’ll be sure to get an example project for Monday.