Any idea why this isn't working?

Here is the code for my DroneController script

So what happens is, when a user clicks on an asteroid, a drone is instantiated, and it has this script attached to it.
Then using SendMessage, the gameobject for the asteroid is sent to the harvest function in this script.

When the harvest function is called, it prints “Message Recieved” to the log (this works), it assigns the object to a new variable name (this works), it sets moving to true, and prints the location of the asteroid to the log screen (this also works).

What is not working, is moving is not getting set to true.
If I manually set moving to true in the inspector, then the movement code works. I shouldnt have to do that though. As soon as the harvest function is called moving should be set to true.

Any ideas?

At a glance, aren’t you setting the DroneController variable moving to true instead of the one contained within the passed GameObject in the harvest method signature?

I’m not sure.

The way it should work is like this:

Drone GameObject is instantiated with DroneController.cs attached to it.
Bool moving is declared.
Bool moving is set to false on Start()

Since moving is false, nothing happens in Update()

An outside script calls harvest() and sends it a gameobject (the asteroid selected)
moving is set to true
the GameObject ast is set to the GameObject passed to harvest()

now that moving is true, everything in the if/then section of update() should start running.

As best as I can tell, even though the moving inside the harvest() function is being set to true, its not being recognized by the if/then statement in the Update() function.

I really am curious why it doesn’t work… Seems ok from the code given… May i ask how you call the SendMessage? As you require an object for the “void harvest(GameObject obj)” , my question would be if you call it like this?

void Example() {
gameObject.SendMessage(“ApplyDamage”, 5.0F); // first method name as string, then the given value?

Another thing i would try out is to Debug.Log the bool moving just to be sure everything works out just fine.

I dont have the exact code on me as I’m at work right now.

When I click on an asteroid, I use raycast hit to get the gameobject clicked on. The code will look something like this:

		Ray ray = Camera.main.ScreenPointToRay (Input.mousePosition);
		RaycastHit hit;
		if (Physics.Raycast (ray, out hit)) {
                         drone = Instantiate(prefab, new Vector3(-1f, 0, 0), Quaternion.identity);
		     drone.SendMessage("harvest", hit);

I’ll do some more testing when I get home later. I do know that the harvest function is being called properly, because it does print the Vector3 position of the clicked on asteroid to the debug log. I’ll have to have it print the bool status to the debug log as well when I’m home…

How many drones are you instantiating? If more than one, try changing it so that there is only one, its hard to see how from the above but I wondered whether additional controllers are some how resetting your Moving status.

Aside from that, I would probably change the if starement so that it reads if (moving == true), not entirely sure this will make a difference as I believe your short hand method should be fine but it would at least rule it out. E.g. is moving an object equals true, compared to, does the value of moving equal true.

It’ll produce a lot of output but popping a debug.log in the top of that update method and outputting the value of moving may be useful to.

Just a single one.

I’ll make the suggested changes when I get home and post the results.

Thank you both.

1 Like

I figured out what it is, thanks to your questions, and suggestions on debug statements.

My problem was, I always thought Start() was called when the object was instantiated. Apparently, that is not the case.

With enough debug.log statements, I found out the order the script was really running was:


So, my harvest() script would set moving to true, Start() would set it to false, and Update() never did anything.

I fixed it by changing Start() to Awake().

Thank you both for your help!

1 Like

hehe, nicely spotted! Not sure I help to be honest but really glad you have sorted it! :slight_smile:

Oh trust me, you helped.
Your suggestion of putting a debug statement at the top of the update method is what did it. I did that, and debugs in every function and looked at which order they printed in.

So now my drone instantiates, moves to the asteroid, then once it touches the asteroid moves back to the ship, (and so on at this time)

Now I’ll be able to work out the rest of this script. Progress!

1 Like

Great news! Glad you’ve got it sorted Chris :slight_smile:

Privacy & Terms