Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
1
Question by SGPascoe · May 14, 2012 at 02:57 PM · transformvariablestargetcallingchanging

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.

Comment
Add comment · Show 2
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image perchik · May 14, 2012 at 03:09 PM 1
Share

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

avatar image SGPascoe · May 14, 2012 at 03:30 PM 0
Share

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.

1 Reply

· Add your reply
  • Sort: 
avatar image
0

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

Comment
Add comment · Show 2 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image SGPascoe · May 14, 2012 at 04:16 PM 0
Share

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.

avatar image SGPascoe · May 15, 2012 at 12:34 AM 0
Share

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

Hint: You can notify a user about this post by typing @username

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

Answers Answers and Comments

6 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image

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


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges