• 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 jpmythic , developer, programmer, technician · May 26, 2011 at 01:04 PM · gameobjecttransformcolliders

transform.root.collider From Fps Tutorial issue when organized in view

Was working my way thru the FPS tutorial and ran into and issue with the Fire() function when I did the following:

Properly created the Robot, Created the Robot prefab, no issues Player prefab is working correctly..

Heres the issue, the RocketLauncher.js script does the following:

function Fire () {

 // Did the time exceed the reload time?
 if (Time.time > reloadTime + lastShot && ammoCount > 0)
 {
     // create a new projectile, use the same position and rotation as the Launcher.
     var instantiatedProjectile : Rigidbody = Instantiate (projectile, transform.position, transform.rotation);
         
     // Give it an initial forward velocity. The direction is along the z-axis of the missile launcher's transform.
     instantiatedProjectile.velocity = transform.TransformDirection(Vector3 (0, 0, initialSpeed));

     // Ignore collisions between the missile and the character controller
     Physics.IgnoreCollision(instantiatedProjectile.collider,transform.root.collider);

....

I like organization and keeping things tidy so I Created an Empty GameObject Renamed it [Enemy AIs] Added 2 Enemy robots Then ran the Game..

Error console reads the following:

MissingComponentException: There is no 'Collider' attached to the "Enemy AIs" game object, but a script is trying to access it.

You probably need to add a Collider to the game object "Enemy AIs". Or your script needs to check if the component is attached before using it.

UnityEngine.Physics.IgnoreCollision (UnityEngine.Collider collider1, UnityEngine.Collider collider2) (at C:/BuildAgent/work/6bc5f79e0a4296d6/Runtime/ExportGenerated/Editor/NewDynamics.cs:358)

RocketLauncher.Fire () (at Assets/FPS Tutorial Assets/WeaponScripts/RocketLauncher.js:19)

UnityEngine.Component:BroadcastMessage(String) $:MoveNext() (at Assets/FPS Tutorial Assets/WeaponScripts/AI.js:69)

===================================================================

Its Accessing the ROOT GameObject [Enemy AIs] Not the Root Object inside [Robot 1]

So I was wondering what would be a most Efficient way to Make sure the scripts Access the correct Collider as the call GetComponent(CharacterController)
didnt work in that situation either as the heirarchy of this is the Following:

Enemy AIs -> Robot 1 -> gun -> gun_spawn

Tried the following... transform.GetComponent(CharacterController).collider //Got the gun_spawn object transform.root.GetComponent(CharacterController).collider //Got the Enemy AIs object

and GetComponent returns:

MissingComponentException: There is no 'CharacterController' attached to the "gun_spawn" game object, but a script is trying to access it.

You probably need to add a CharacterController to the game object "gun_spawn". Or your script needs to check if the component is attached before using it.

(wrapper dynamic-method) UnityEngine.CharacterController.CharacterController$get_collider$ (object,object[]) Boo.Lang.Runtime.RuntimeServices.Dispatch (object,string,System.Type[],object[],Boo.Lang.Runtime.DynamicDispatching.DispatcherCache/DispatcherFactory) Boo.Lang.Runtime.RuntimeServices.Dispatch (object,string,object[],Boo.Lang.Runtime.DynamicDispatching.DispatcherCache/DispatcherFactory) Boo.Lang.Runtime.RuntimeServices.GetProperty (object,string) UnityScript.Lang.UnityRuntimeServices.GetProperty (object,string)

RocketLauncher.Fire () (at Assets/FPS Tutorial Assets/WeaponScripts/RocketLauncher.js:20)

UnityEngine.Component:BroadcastMessage(String) $:MoveNext() (at Assets/FPS Tutorial Assets/WeaponScripts/AI.js:69)

===================================================================

The Character Controller is attached to [robot 1] The script is connected to the [gun_spawn] This was after following thru the tutorial into part 3...

I was seriously thinking of creating a var in the root [robot] prefab that was nothing more then a link to itself (the main object with controller) and then accessing that var instead...

var objectRoot : Transform;

then just drop itself into the link and try accessing it by objectRoot.collider

as I did some looking into this in reference section and using GetComponentInChildren(..) would probably return a random one not the correct one if there is more then [1] robot in the Root gameObject...

Linking gameObjects can be a real brain teaser lol and I want to make sure I do this properly so I dont end up with hard to track down bugs later :(

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

1 Reply

· Add your reply
  • Sort: 
avatar image
0

Answer by jpmythic , developer, programmer, technician · May 26, 2011 at 01:17 PM

Tried out the following:

in AI.js added

var objectAI : Transform;

In RocketLauncher.js changed it to this:

 function Fire ( ownerAI : Transform )

{ // Did the time exceed the reload time? if (Time.time > reloadTime + lastShot && ammoCount > 0) { // create a new projectile, use the same position and rotation as the Launcher. var instantiatedProjectile : Rigidbody = Instantiate (projectile, transform.position, transform.rotation);

     // Give it an initial forward velocity. The direction is along the z-axis of the missile launcher's transform.
     instantiatedProjectile.velocity = transform.TransformDirection(Vector3 (0, 0, initialSpeed));

     // Ignore collisions between the missile and the character controller
     if( ownerAI == null )
     {
         Physics.IgnoreCollision(instantiatedProjectile.collider,transform.root.collider);
     }
     else
     {
         Physics.IgnoreCollision(instantiatedProjectile.collider, ownerAI.collider);
     }


And that works with Two AIs in [Enemy AIs] and the Player (using the same Fire() function without sending ownerAI) The Fire( ownerAI ) is called in the AI.js

I linked the robot prefab to the script var ownerAI

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.

Welcome to Unity Answers

If you’re new to Unity Answers, please check our User Guide to help you navigate through our website and refer to our FAQ for more information.

Before posting, make sure to check out our Knowledge Base for commonly asked Unity questions.

Check our Moderator Guidelines if you’re a new moderator and want to work together in an effort to improve Unity Answers and support our users.

Follow this Question

Answers Answers and Comments

1 Person is following this question.

avatar image

Related Questions

Translate.transform problems (collision/Rigibody) 0 Answers

"Transform" still trying to access after Destroy 0 Answers

SetActiveRecursively not activating children 2 Answers

How to make a object translate alonge an other 2 Answers

How to assign random positions to gameobjects from an array? 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