
It looks like you're new here. If you want to get involved, click one of these buttons!
I have 2 scripts, 1 is attached to my player empty game object and the other is attached to my unit prefab. The unit prefab has NavMeshAgent attached and cap collider, Rigidbody3d, ect...
problem...
on script 1 I am using a RaycastHit to identify colliders and a debug.log to see what colliders are being hit. I am also calling the other script at the top to modify a few things. I am not getting any errors or warnings in unity or visual studio.
1. I need to modify a bool "isSelected" to true. 2. I need to pass a Vector3 from "hit.point" to the second script as the destination.
Here is how script 1 is doing that.
Script2 script2;
RaycastHit hit;
Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);
if(physics.Raycast(ray, out hit, 100)
{ if(hit.collider.tag == "unit")
{script2. isSelected = true;}
if{hit.collider.tag == "ground" && Input.GetMouseButton (0))
{script2. destination = hit.point;}}
script 2...
needs to accept the input and move the unit. here is the basic set up.
public NavMeshAgent agent;
public bool isSelected = false;
public Vector3 destination;
if(isSelected)
{agent.SetDestination = destination;
agent.Transform.LookAt(Destination);
}
I know i left a lot out because im messaging this on my phone. if you need to see the whole script please let me know. Again, when i run it, it does not show any errors or warnings. While its running, when trying to click the unit, debug.log shows that the ray is hitting the collider for the unit, but it does not change the isSelected to true on script2 or if I select the ground, again debug.log shows that its hitting the ground but nothing is passing to script2. the unit is not moving or anything . I have even debuged to see if anything is attemting to pass and nothing.
any suggestions or help is welcome. Sorry that i skipped the method names and ect, it would take 30 mins to do it on my phone but in the script they are all in methods and update calls the methods.
What reference did you give in the first line of script in
Script2 script2;
I think you have given a prefab reference here and in the runtime you instantiates the prefab and you want to modify that thing.
But this will not work cuz the script1 has reference to a prefab not existing in runtime. So you have to give correct reference maybe this will work.
Answers
are you meaning as far as reference? in script 2 i dont have any reference to script 1 from script 2. I will try it with a reference to script 1.
Ok, that worked thank you but it gave me another problem. Now when i select any unit, every single unit of the same type is selected and move to the target. How can I make it only select that one specific unit?