Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 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
0
Question by IMTRIGGERHAPPY9 · Aug 04, 2011 at 07:05 PM · transformtriggerlookattower-defense

LookAt Trigger for multiple objects

the below script is my script that i use to detect if the gameobject has a tag of enemy: SCRIPTS NAME IS: attackRTriggerStay.js

 @HideInInspector 
 var targetAquired = false;
 
 function OnTriggerEnter (other : Collider) {
     
     print(targetAquired);
     
     if(other.gameObject.CompareTag ("enemy")){
         targetAquired = true;
         print("targetAquired");
     }
     
     else {
     targetAquired = false;
     print(targetAquired);
     }
     
     
 }

i want the below script to edit a different object from the attack radius.

SCRIPTS NAME: LookAt.js

 function Update () {
         if( attackRTriggerStay.targetAquired == true)
             transform.LookAt(attackRTriggerStay.other.transform.position);
     
     
     }

my code on top is working perfectly but then when i try to compile with the second code its giving me this error AN INSTANCE OF TYPE'attackRTriggerStay' is required to acces non static memeber 'targetAquired'

so all that i am looking for is for my bottom script to recognize the value of targetAquired from the top script

Comment
Add comment · Show 12
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 IMTRIGGERHAPPY9 · Aug 04, 2011 at 07:45 PM 0
Share

anybody help?

avatar image superventure · Aug 04, 2011 at 07:57 PM 0
Share

Do you mean to use
function OnTriggerStay(other.collider){ if(other.name == "target"){
transform.LookAt(other.transform.position); }}

I am just guessing here, but I'm not sure of your goal. I Need more code to help further

avatar image IMTRIGGERHAPPY9 · Aug 04, 2011 at 08:04 PM 0
Share

i edited it to make more sense sorry for the confusion

avatar image superventure · Aug 04, 2011 at 08:19 PM 0
Share

That means 'attackRTriggerStay' is undefined. The editor doesn't know what it is or what to do with it- is it a game object? a tag? a name?

avatar image SisterKy · Aug 04, 2011 at 10:57 PM 1
Share

Have you tried adding my code to your LookAt.js above the function Update? (of course you have to put the name of an acctual Object ins$$anonymous$$d of THeObjectThatattackRTriggerStayIsAttatchedTo....)

As a sidenote: you should start Scriptnames and Functionnames with uppercase. Variables with lowercase. (People will be confused if you don't stick to that convention)

Greetz, $$anonymous$$y.

Show more comments

2 Replies

· Add your reply
  • Sort: 
avatar image
2
Best Answer

Answer by SisterKy · Aug 04, 2011 at 11:49 PM

Currently, since you don't tell Unity where it should look for 'attackRTriggerStay' it just takes 'the Script' instead of 'this Script over here'
You need to tell it where to find the Script.

So you have several towers.
each tower has its own LookAt.js
each tower has its own child-Cylinder with a attackRTriggerStay.js ?
no tower has more than one child with attackRTriggerStay.js ?

Ok then...

Edit: I realized another problem. You won't be able to access 'other' in this line:
transform.LookAt(attackRTriggerStay.other.transform.position);

Add this to attackRTriggerStay.js:

var targetAquired = false; var theTarget : Transform;

function OnTriggerEnter (other : Collider) {

theTarget = other.transform;

So now in LookAt.js you can do:

var myAttackRadius : attackRTriggerStay;

 function Start () 
 {
    myAttackRadius = GetComponentInChildren(attackRTriggerStay);
 }

 function Update () 
 {
     if(myAttackRadius.targetAquired == true)
         transform.LookAt(theTarget.position);
 }

I think this should do...

Note on 'other':
The reason we have to assign 'other.transform' to the 'theTarget'-variable is, that 'other' only exists inside the OnCollisionEnter-Function. You can't access it from the outside.

Note on 'Start':
The thing I do in Start () means I look for the PATH to your other SCRIPT. While the variable 'targetAquired' will constantly change, and while the actual target will constantly change, the path to where this variable can be found will always be the same.

Extended Note on 'Start':

there are 2 general types Variables


value type: holds a number or word etc.

  • boolean

  • int

  • float

  • string


reference type: holds the path to the Instance of anything derived from UnityEngine.Object
(Instance means a copy of a script when it's attached to a GameObject)

  • Transform

  • GameObject

  • Collider

  • basically anything listed under ('derived from') 'Object' in the list of RuntimeClasses (jep, really all of those!)

  • MyOwnClass(=MyOwnScriptname)

note: you can not reference a value type


e.g. you have GameObjectA with a ScriptA with a ValueA=12 and want to access it from GameObjectB


VariableB = PathToGameObjectA.PathToScriptA.ValueA;
// this assigns the *content* of ValueA (=12)
VariableB += 5; 
// this adds 5 to 12 = 17 and assigns it to VariableB; ValueA does not change.
vice versa, if ValueA is changed by ScriptA, VariableB stays 17 unless you repeat the above assignment

VariableB = PathToGameObjectA.PathToScriptA;
// this assigns the *Path* to ScriptA
VariableB.ValueA += 5;
// this adds 5 to 12 = 17 and assigns it to ValueA
If ValueA is changed by ScriptA, VariableB.ValueA knows about it.

So the second part is what I used in Start()
the variable 'myAttackRadius' references 'this attackRTriggerStayScript over here'.
then in Update we always look at the addressed Script if its Variables got updated and take appropriate actions to kill that evil intruder :p ^^

Greetz, Ky.

Comment
Add comment · Show 4 · 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 IMTRIGGERHAPPY9 · Aug 05, 2011 at 12:06 AM 0
Share

@Sister$$anonymous$$y sweet i am very thankful for the help, but your going to have to explain to me what the second LookAt.js script is doing in more depth as i am not that advance yet. the update makes sense but the function start and my attack radius are confusing me.

the first part was genius though i would have never thought to make other be defined by another variable so i could define it easier!

avatar image SisterKy · Aug 05, 2011 at 12:51 AM 0
Share

Here, take a look at this: http://unityscript.com/ =) Some parts of this may be a bit too basic for you but I think you can still benefit.

Greetz, $$anonymous$$y.

avatar image IMTRIGGERHAPPY9 · Aug 05, 2011 at 09:19 PM 0
Share

that was awesome! the reference was kinda way to basic ha ha but your explaination was spot on

avatar image SisterKy · Aug 06, 2011 at 12:32 AM 0
Share

I know how you feel... Reference is not too basic though... it's too advanced... it expects you to already know all this stuff. Oh, I know so well how you feel...! nods knowingly Glad I could help =)

avatar image
0

Answer by GuyTidhar · Aug 04, 2011 at 08:16 PM

attackRTriggerStay was not instantiated (if it is a monobehaviour class) or constructed (if it a non mono behaviour class). "Instance" is a result of creating an object in memory - holding all relevant data in the code.

Comment
Add comment · 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

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

7 People are following this question.

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

Related Questions

transform.position not executed with OnTrigger2D 1 Answer

What would you do with essential items? 1 Answer

Lock rotation axis? 4 Answers

Transform.LookAt Function Customizations... 1 Answer

Hello, Need some help with Enemy facing the player on z axis !! 0 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