- Home /
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 :(
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
Your answer
Follow this Question
Related Questions
Change target after destroying it. 2 Answers
Write a C# script to record GameObject rotation? 4 Answers
Is transform another name for a gameObject? 2 Answers
Can't skew a gameobject transform? 2 Answers
How do I check multiple gameObjects transform positions? 3 Answers