x


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.

more ▼

asked May 14 '12 at 02:57 PM

SGPascoe gravatar image

SGPascoe
20 5 6 9

Can you show us what you're actually doing (as opposed to what you want to happen?)

May 14 '12 at 03:09 PM perchik

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.

May 14 '12 at 03:30 PM SGPascoe
(comments are locked)
10|3000 characters needed characters left

1 answer: sort voted first

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;
  }
  ...
more ▼

answered May 14 '12 at 04:01 PM

aldonaletto gravatar image

aldonaletto
41.5k 16 42 197

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.

May 14 '12 at 04:16 PM SGPascoe

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#?

May 15 '12 at 12:34 AM SGPascoe
(comments are locked)
10|3000 characters needed characters left
Your answer
toggle preview:

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this question

By Email:

Once you sign in you will be able to subscribe for any updates here

By RSS:

Answers

Answers and Comments

Topics:

x1280
x344
x243
x25
x21

asked: May 14 '12 at 02:57 PM

Seen: 724 times

Last Updated: May 15 '12 at 12:34 AM