- Home /
Change transform variable from outside
So I have an AIFollow.cs script for the A* pathfinding, which needs a target to follow. Currently it finds the player transform.
I also have a Trigger.js script on a child object I'm using to choose the target.
For example of what I want to achieve in that script:
If (trigger == false) {AIFollow.target = null}
If (trigger == true) {AIFollow.target = transform.Find("Player"}
But I can't find the right method to set this! I've been looking through Answers and the script reference, but everything I try just comes up with errors.
Can you show us what you're actually doing (as opposed to what you want to happen?)
Well I have a trigger boolean, set by a Linecast used for sighting.
If the linecast hits the player object, I want the target of the AIFollow script to be the player. If the linecast hits any other object, I want it to set the AIFollow target to null.
I have the script all ready with the outcome of the linecast to be: (sighted == true) or (sighted == false).
I now need to set
If (sighted == false) {AIFollow.target = null} If (sighted == true) {AIFollow.target = transform.Find("Player"}
for my entire script to work. I've been trying to get it to work for a while but keep encountering errors.
Answer by aldonaletto · May 14, 2012 at 04:01 PM
You can check visibility using a linecast and verifying if the hit object is the target (is visible) or something else (not visible) - but you should save the target in some variable: how could you know what to search if the target is null? Something like this (script attached to the enemy):
var player: Transform; // save the player here at Start
function Start(){ player = GameObject.Find("Player").transform; }
... var hit: RaycastHit; if (Physics.Linecast(transform.position, player.position, hit) && hit.transform == player){ AIFollow.target = player; } else { AIFollow.target = null; } ...
This is pretty much exactly what I have now, but it says that AIFollow cannot be found. the same happens when I use
var aifollow = AIFollow aifollow.target = player
or var enemy = Transform (selecting enemy) enemy.AIFollow.target = player
I think the problem is that the script is being run on a child object rather than the same object that the AIFollow script is on. it needs to be this way too, as there are other parts of the script that needs the collision trigger of the child object.
I have gone further to add a script to the parent object, and the AIFollow script still cannot be found and does not automatically pop up like other scripts would in the correct useage. is this because it is C#?
Your answer
![](https://koobas.hobune.stream/wayback/20220613071455im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
Rotating Y towards X/Z 1 Answer
Assign parent object to target 2 Answers
What is wrong with this code? 0 Answers
Need help with transform.LookAt(target) 1 Answer
deactivate/activate child/separate prefab from script attached to other object 2 Answers