Search Unity

  1. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

Survival Shooter Q&A

Discussion in 'Community Learning & Teaching' started by Adam-Buckner, Mar 26, 2015.

Thread Status:
Not open for further replies.
  1. IutbaZion

    IutbaZion

    Joined:
    May 22, 2015
    Posts:
    2
    hi, im getting this error NullReferenceException: Object reference not set to an instance of an object PlayerHealth.Death () (at Assets/Scripts/Player/PlayerHealth.cs:76) im using the same code as the tutorial
    i dont know if its important, but im on the sixth part of the tutorial and the Character makes the death animation but it keeps moving
    Code (CSharp):
    1. using UnityEngine;
    2. using UnityEngine.UI;
    3. using System.Collections;
    4.  
    5. public class PlayerHealth : MonoBehaviour
    6. {
    7.     public int startingHealth = 100;
    8.     public int currentHealth;
    9.     public Slider healthSlider;
    10.     public Image damageImage;
    11.     public AudioClip deathClip;
    12.     public float flashSpeed = 5f;
    13.     public Color flashColour = new Color(1f, 0f, 0f, 0.1f);
    14.  
    15.  
    16.     Animator anim;
    17.     AudioSource playerAudio;
    18.     PlayerMovement playerMovement;
    19.     //PlayerShooting playerShooting;
    20.     bool isDead;
    21.     bool damaged;
    22.  
    23.  
    24.     void Awake ()
    25.     {
    26.         anim = GetComponent <Animator> ();
    27.         playerAudio = GetComponent <AudioSource> ();
    28.         playerMovement = GetComponent <PlayerMovement> ();
    29.         //playerShooting = GetComponentInChildren <PlayerShooting> ();
    30.         currentHealth = startingHealth;
    31.     }
    32.  
    33.  
    34.     void Update ()
    35.     {
    36.         if(damaged)
    37.         {
    38.             damageImage.color = flashColour;
    39.         }
    40.         else
    41.         {
    42.             damageImage.color = Color.Lerp (damageImage.color, Color.clear, flashSpeed * Time.deltaTime);
    43.         }
    44.         damaged = false;
    45.     }
    46.  
    47.  
    48.     public void TakeDamage (int amount)
    49.     {
    50.         damaged = true;
    51.  
    52.         currentHealth -= amount;
    53.  
    54.         healthSlider.value = currentHealth;
    55.  
    56.         playerAudio.Play ();
    57.  
    58.         if(currentHealth <= 0 && !isDead)
    59.         {
    60.             Death ();
    61.         }
    62.     }
    63.  
    64.  
    65.     void Death ()
    66.     {
    67.         isDead = true;
    68.  
    69.         //playerShooting.DisableEffects ();
    70.  
    71.         anim.SetTrigger ("Die");
    72.  
    73.         playerAudio.clip = deathClip;
    74.         playerAudio.Play ();
    75.  
    76.         playerMovement.enabled = false;
    77.         //playerShooting.enabled = false;
    78.     }
    79.  
    80.  
    81.     public void RestartLevel ()
    82.     {
    83.         Application.LoadLevel (Application.loadedLevel);
    84.     }
    85. }
    86.  
     
    Last edited: May 22, 2015
    ZEBRAstudio likes this.
  2. Oceankingdom

    Oceankingdom

    Joined:
    May 21, 2015
    Posts:
    3
    unity shutdown when i import line material. who known why ?
     
  3. RepVis

    RepVis

    Joined:
    Nov 23, 2012
    Posts:
    2
    I'm having the same problem. Enemies cast shadows when playing in editor, but not in a built exe. My player still casts shadows, but not the enemies. I tried manually adding a couple of the enemy prefabs to the hierarchy to rule out anything related to the instantiations, but those did not cast shadows either.

    Edit: Looking more closely, the eyes of the bears and bunnies are casting shadows. Their bodies are not, however.
     
  4. ZEBRAstudio

    ZEBRAstudio

    Joined:
    May 23, 2015
    Posts:
    2
    I am also getting the same error plz ASAP

    NullReferenceException:Object reference not set to an instance of anobject PlayerHealth.Death () (at Assets/Scripts/Player/PlayerHealth.cs:76

     
  5. RepVis

    RepVis

    Joined:
    Nov 23, 2012
    Posts:
    2
    Ok, I've found the source of the problem with the shadows; it's the custom shader the enemies are using, Rim Lit Bumped Specular. If I change to a standard material they begin casting shadows properly, but obviously the very cool rim lighting effect is lost. Looks like it's time to start researching shader programming.

    Edit: Found the fix. Changing the Fallback in the Custom/Rim Lit Bumped Specular shader from "Specular" to "Standard" resolves the shadow problem.
     
    Last edited: May 24, 2015
  6. ZEBRAstudio

    ZEBRAstudio

    Joined:
    May 23, 2015
    Posts:
    2
    NullReferenceException:Object reference not set to an instance of anobject PlayerHealth.Death () (at Assets/Scripts/Player/PlayerHealth.cs:76


    I have found a fix i copied the player movement script staright from the website. Suprisingly it fixed it. It has something to do with that
     
  7. ArmanOhanian

    ArmanOhanian

    Joined:
    May 2, 2015
    Posts:
    6
    For the fun of it, I took this tutorial mobile and built an Android version. I used the CrossPlatformInputManager to get the tilt controls, etc etc...

    It plays fine on the PC and in the editor with Unity Remote. However, when I install it on my Android devices, the performance is horrible! I have tried this across 3 different devices and none performed: Moto G (2nd gen), Galaxy Tab 2, and Blu (something)...

    While I may have a clue why that is (guessing high-vertix count models?), I'd like here your professional opinion.

    Thanks!
     
  8. djchallis

    djchallis

    Joined:
    May 20, 2015
    Posts:
    3
    If anyone is still having the issue at the end of video 4 where the enemy always runs to (0.0, 0.0, 0.0) instead of chasing the player then restarting Unity might fix it. That's what this thread recommended and it worked for me. Since that would make this a Unity bug I thought I'd post my findings in case it helps.

    Debugging this was confused by the fact that our Player object's transform starts at (0.0, 0.0, 0.0), which is also what the transform variable "player" in EnemyMovement.cs will end up as if FindWithTag fails to find the Player. I set the Player's starting point to somewhere else and ended up with these test results:

    When using "GameObject.Find" it chases the Player correctly. The initial value of player.position is my Player's new starting position, and player.gameObject is "Player".
    When using "GameObject.FindWithTag" it doesn't chase the Player. The initial value of player.position is (0.0, 0.0, 0.0), and player.gameObject is "Player(Clone)".

    This Player(Clone) object seemed just liked the objects described in the link I mentioned above. Presumably it was being created somehow and the FindWithTag was finding it instead of the actual Player. Now that I've restarted Unity FindWithTag gives the same results as using Find, and I can't find Player(Clone) anywhere.
    I'm using Unity 5.0.2f1 Personal, by the way.
     
  9. IutbaZion

    IutbaZion

    Joined:
    May 22, 2015
    Posts:
    2
    ty mate, i will try this later :D
     
  10. Sagehorn

    Sagehorn

    Joined:
    May 17, 2014
    Posts:
    2
    Hey, thanks for that great tutorial!
    I added my own enemy model by copying an existing and changing the relevant entries. Everything works fine with two exceptions:

    1. The enemy dies but the model doesn't sink through the ground.
    2. The player doesn't get points after killing.

    Seems like the game doesn't register the enemy is dead? Afaik there is no code that has to be edited for including new enemies.. So what could it be? Any idea?
     
  11. Adam-Buckner

    Adam-Buckner

    Joined:
    Jun 27, 2007
    Posts:
    5,664
    Null reference exceptions mean that you are missing a reference to an object. In your case, Unity is looking for PlayerHealth.Death (), but can't find it. This can be caused by a number issues from a typo to deleting a script by accident. Even you see a null ref, you need to track down why an item is missing or not linked when the project is running.

    Looking at your origional post:
    This suggests that the error is on line 76. Your posted code does not have this call at line 76. Is this the correct code and the correct error that you have pasted?
     
  12. Adam-Buckner

    Adam-Buckner

    Joined:
    Jun 27, 2007
    Posts:
    5,664
    Can you please give us any more information? Is this consistent? Can you reproduce this 100% with the same material? Or was this a one-time event?
     
  13. kyleunruh

    kyleunruh

    Joined:
    May 11, 2015
    Posts:
    17
    Hello!

    I'm having a minor issue with baking the NavMesh. I am getting little circular anomalies where no objects are present (see attached screenshot). I've tried tweaking the settings a bit and also used @peteorstrike's settings exactly, but there are still small spots where the enemy can't move.

    Anyone else have this issue and/or know a possible fix? I have a feeling it has something to do with the cracks in the Floor of the Environment prefab, but I'm not sure how to fix it.
     

    Attached Files:

  14. Zamas

    Zamas

    Joined:
    Apr 15, 2015
    Posts:
    35
    The only solution is, I think, for you to continue to change your navmesh setting, especialy the voxel size, and the agent setting. You'll still end up with some really small part of the navmesh not working properly.
     
    kyleunruh likes this.
  15. kyleunruh

    kyleunruh

    Joined:
    May 11, 2015
    Posts:
    17
    Thanks @Zamas, I went back and played with the settings some more and ended up with a pretty decent NavMesh:

    scene_navmesh.png

    I started thinking more about the voxel size and realized the accuracy was actually too high and therefore the mesh was baking down into the cracks in the floor. I increased the voxel size (about 3.75 voxels per agent) and it worked much better. Here are my bake setting for anyone else that might have the same problem:

    bake_settings.PNG

    I decided it was fine to ignore the warning about the Step Height and Max Slope conflict since there aren't any significant slopes for the enemies to traverse anyway. And lowering the Max Slope too much made the NavMesh run over some of the small objects on the floor.

    One last note: I moved the alarm clock a little bit further away from the dresser because the space was too narrow for Agents to pass between, but the player could move between them just fine. I changed this because I felt this had potential to be exploited by a player.
     
  16. Wabefuhon

    Wabefuhon

    Joined:
    May 27, 2015
    Posts:
    13
    Ookay. I got a problem. I've connected the animations like video 2 and this is my code.
    Code (CSharp):
    1. using UnityEngine;
    2.  
    3. public class PlayerMovement : MonoBehaviour{
    4.     public float speed = 6f;
    5.     Vector3 movement;
    6.     Animator anim;
    7.     Rigidbody prb;
    8.     int floorMask;
    9.     float camRayLength = 100f;
    10.     void Awake(){
    11.         floorMask = LayerMask.GetMask ("Floor");
    12.         anim = GetComponent<Animator> ();
    13.         prb = GetComponent<Rigidbody> ();
    14.     }
    15.     void FixedUpdate(){
    16.         float h = Input.GetAxisRaw ("Horizontal");
    17.         float v = Input.GetAxisRaw ("Vertical");
    18.         Move (h, v);
    19.         Turning ();
    20.         Animating (h, v);
    21.     }
    22.     void Move(float h, float v){
    23.         movement.Set (h, 0f, v);
    24.         movement = movement.normalized * speed * Time.deltaTime;
    25.         prb.MovePosition (transform.position + movement);
    26.     }
    27.     void Turning(){
    28.         Ray camRay = Camera.main.ScreenPointToRay (Input.mousePosition);
    29.         RaycastHit floorHit;
    30.         if (Physics.Raycast (camRay, out floorHit, camRayLength, floorMask)) {
    31.             Vector3 playerToMouse = floorHit.point - transform.position;
    32.             playerToMouse.y = 0f;
    33.             Quaternion newRotation = Quaternion.LookRotation (playerToMouse);
    34.             prb.MoveRotation (newRotation);
    35.         }
    36.     }
    37.     void Animating(float h, float v){
    38.         bool walking = h != 0f || v != 0f;
    39.         anim.SetBool ("IsWalking", walking);
    40.     }
    41. }
    42.  
    I have not received an error. However, my character does not do the walking animation or follow the mouse. The idle animation is running, overtime the walking animation will run and there is no audio, even though the transparent audio icon is visible in the Scene window.

    I don't have annotations opened, so I don't know if there are any changes in this tutorial. Noticed people were talking about it and wondered myself. I haven't checked yet.
     
  17. kyleunruh

    kyleunruh

    Joined:
    May 11, 2015
    Posts:
    17
    @Wabefuhon, when you are making your state transitions (Idle -> Move and Move -> Idle), make sure you uncheck the 'Has Exit Time' option. This should allow the transitioned animation to "interrupt" the previous animation instead of waiting for it to finish.

    Also, you code looks fine...maybe double check that the PlayerMovement script has been added to the Player object? It should show up in the Inspector tab when you have the Player selected.
     
    Last edited: May 28, 2015
  18. Wabefuhon

    Wabefuhon

    Joined:
    May 27, 2015
    Posts:
    13
    @kyleunruh Where would I find "Has Exit Time"?
     
  19. kyleunruh

    kyleunruh

    Joined:
    May 11, 2015
    Posts:
    17
    @Wabefuhon Locate and select your Player game object in the Hierarchy tab. Now, in the Inspector window, you should see the "Animator" component (see below).

    player_selected.PNG

    In the Animator component, Controller should be set to 'PlayerAC' (or whatever you named your Animator Controller). If you double click PlayerAC, it should open up in a new tab that shows the state machine. If you click one of the transition arrows connecting the Idle state and Move state, you should see all the options for that transition in the Inspector tab on the right. The 'Has Exit Time' checkbox is there (boxed in red in the image below).

    has_exit_time.PNG

    Also, make sure you uncheck the option for both transitions ('Idle to Move' and 'Move to Idle').
     
  20. Wabefuhon

    Wabefuhon

    Joined:
    May 27, 2015
    Posts:
    13
    @kyleunruh Thanks. Really helped. Except that my character doesn't follow the mouse cursor still.
     
  21. kyleunruh

    kyleunruh

    Joined:
    May 11, 2015
    Posts:
    17
    @Wabefuhon Hm, when you say doesn't follow, do you mean he doesn't rotate towards the mouse cursor? Or your character wont move at all? The way they designed the controls is a little funky to get used to IMO. The cursor is just used to turn the Player, but all the movement is controlled by the WASD or Arrow keys. So if you are holding the up key, you will always be moving towards the top of the screen, regardless of the direction your character is facing.

    Not sure if what I said is very clear...basically if your cursor is over the play screen, the Player should just be looking at it, not moving towards it. Does he do that, or is he always facing the same direction?
     
  22. Wabefuhon

    Wabefuhon

    Joined:
    May 27, 2015
    Posts:
    13
    @kyleunruh I can move, but the player ignores the mouse.
     
  23. kyleunruh

    kyleunruh

    Joined:
    May 11, 2015
    Posts:
    17
    @Wabefuhon Hmm, that is odd. I copied your code for the PlayerMovement script and pasted it over mine and it works perfectly for me.

    When you press play, are you moving your mouse cursor over the 'Game' window? If you try moving it around over the 'Scene' view, it wont do anything.

    Also, another thing to check is make sure your Camera game object is tagged as "MainCamera". Apparently the line
    Code (CSharp):
    1. Ray camRay = Camera.main.ScreenPointToRay (Input.mousePosition);
    will only work for the camera object that is tagged with "MainCamera" (See image below).

    maincamera.PNG
     
  24. Wabefuhon

    Wabefuhon

    Joined:
    May 27, 2015
    Posts:
    13
    @kyleunruh It is tagged, yet I have the problem. Are you using Unity 4.6 or 5, PC or MAC?

    Using Unity 5 on Windows 7.
     
  25. kyleunruh

    kyleunruh

    Joined:
    May 11, 2015
    Posts:
    17
    @Wabefuhon I am also using Unity 5 on Windows 7.

    If you are able to move your player but just not rotate him, I would assume the problem must be somewhere in the Camera settings, Player settings, or one of their scripts. Here is a screen shot of all my camera settings and my player settings:

    camera_settings.PNG player_settings.PNG

    Also, just to be thorough, here are my PlayerMovement and CameraFollow scripts:
    Code (CSharp):
    1. using UnityEngine;
    2.  
    3. public class PlayerMovement : MonoBehaviour
    4. {
    5.     public float speed = 6f;
    6.  
    7.     private Vector3 movement;
    8.     private Animator anim;
    9.     private Rigidbody playerRigidBody;
    10.     private int floorMask;
    11.     private float camRayLength = 100f;
    12.  
    13.     void Awake()
    14.     {
    15.         floorMask = LayerMask.GetMask("Floor");
    16.         anim = GetComponent<Animator>();
    17.         playerRigidBody = GetComponent<Rigidbody>();
    18.     }
    19.  
    20.     void FixedUpdate()
    21.     {
    22.         float h = Input.GetAxisRaw("Horizontal");
    23.         float v = Input.GetAxisRaw("Vertical");
    24.  
    25.         Move (h, v);
    26.         Turning ();
    27.         Animating (h, v);
    28.     }
    29.  
    30.     void Move(float h, float v)
    31.     {
    32.         movement.Set (h, 0f, v);
    33.  
    34.         movement = movement.normalized * speed * Time.deltaTime;
    35.  
    36.         playerRigidBody.MovePosition(transform.position + movement);
    37.     }
    38.  
    39.     void Turning()
    40.     {
    41.         Ray camRay = Camera.main.ScreenPointToRay(Input.mousePosition);
    42.  
    43.         RaycastHit floorHit;
    44.  
    45.         if (Physics.Raycast(camRay, out floorHit, camRayLength, floorMask ))
    46.         {
    47.             Vector3 playerToMouse = floorHit.point - transform.position;
    48.             playerToMouse.y = 0f;
    49.  
    50.             Quaternion newRotation = Quaternion.LookRotation(playerToMouse);
    51.             playerRigidBody.MoveRotation(newRotation);
    52.         }
    53.     }
    54.  
    55.     void Animating(float h, float v)
    56.     {
    57.         bool walking = (h != 0f || v != 0f);
    58.         anim.SetBool("IsWalking", walking);
    59.     }
    60. }
    Code (CSharp):
    1. using UnityEngine;
    2. using System.Collections;
    3.  
    4. public class CameraFollow : MonoBehaviour {
    5.  
    6.     public Transform target;
    7.     public float smoothing = 5f;
    8.  
    9.     private Vector3 offset;
    10.  
    11.     void Start()
    12.     {
    13.         offset = transform.position - target.position;
    14.     }
    15.  
    16.     void FixedUpdate()
    17.     {
    18.         Vector3 targetCamPos = target.position + offset;
    19.         transform.position = Vector3.Lerp(transform.position, targetCamPos, smoothing * Time.deltaTime);
    20.     }
    21. }
    22.  
    I'm far from experienced with Unity so if it isn't something in one of those settings, I am kind of at a loss :confused: If it still isn't working, I would imagine its either a bug that has to do with the mouse/hardware you are using or some buried setting in your preferences...
     
  26. Wabefuhon

    Wabefuhon

    Joined:
    May 27, 2015
    Posts:
    13
    @kyleunruh I'm pretty much stuck. I checked all my settings, I copied your scripts, nothings changed. I don't want to give up on Unity. The only other option is Java and RPG Maker VX Ace. I have an Idea. Backup your work and start over. Follow the tutorial exactly as they say and demonstrate. From there, make note of any changes that they do not mention in the first 2 videos. Make sure you get all the details. Maybe I'm missing or need to change something that's being overlooked. In the meantime, I'll see if I can find anything on using different code for using mouse interface.
     
  27. kyleunruh

    kyleunruh

    Joined:
    May 11, 2015
    Posts:
    17
    @Wabefuhon There is one last thing you can check. Make sure you have the 'Floor' game object in your scene (not the environment floor, but an invisible plane for the raycast to bounce off of). They explain to you how do set this up here:
    Code (CSharp):
    1. https://www.youtube.com/watch?v=_lP6epjupJs&feature=youtu.be&t=318
    Make sure that Floor is there and you have the same settings as they show in the video, especially that it is in the Floor layer.

    (PS - Sorry, had to put the link in code because it kept removing the time maker from the link so it starts in the rights spot)
     
    Last edited: May 30, 2015
  28. Wabefuhon

    Wabefuhon

    Joined:
    May 27, 2015
    Posts:
    13
    @kyleunruh I got it. It wasn't working when I edited the floor, but when I deleted and redid the floor, it worked. Thanks.
     
    kyleunruh likes this.
  29. kyleunruh

    kyleunruh

    Joined:
    May 11, 2015
    Posts:
    17
  30. Wabefuhon

    Wabefuhon

    Joined:
    May 27, 2015
    Posts:
    13
    Should I be concerned that there is no Inaccuracy % slider in the Navigation for Unity 5?
     
  31. kyleunruh

    kyleunruh

    Joined:
    May 11, 2015
    Posts:
    17
  32. kyleunruh

    kyleunruh

    Joined:
    May 11, 2015
    Posts:
    17
    I believe there were some people earlier in the thread that have noticed there is a discrepancy between the GameOverManager script from the tutorial video and the one that comes with the Assets download. The script that comes with the download no longer has any of the logic that dictates when to restart the level. But if you were to test the game with this script as it is, the game will still "magically" restart when you die. It took me a stupidly long time to figure out how this was occurring.

    At the bottom of the PlayerHealth script there is a function called RestartLevel which simply calls the Unity function that reloads the current level. But nowhere else in any of the scripts is this called! That is because there is a sneaky little Animation Event hiding in the Player.fbx model called RestartLevel (which, I assume, goes and finds this function and calls it when the event occurs). To find this animation event, navigate to Assets > Models > Characters and select the Player model. In the Inspector tab you will see the "Player Import Settings" which has an Animations tab. Select the "Death" clip, and then expand 'Events' near the bottom which will show a little timeline with a white marker near the end (see image below).

    restart_even_animation.png

    In order to control the restart delay like in the tutorial video, delete that Animation Event and then edit your GameOverManager script to match the one in the video. Just make sure you also delete or comment out the RestartLevel function from the PlayerHealth script too! You can also leave the event there as it works just fine with the original script. However, you have far less control over how much of a delay there is before the level restarts. (It does appear possible to extend the delay of the restart event, but it requires copying and editing animations in the model itself :/)

    Just felt like I should put this here just in case other people that may be confused like I was come searching for an answer!
     
    U_nitty likes this.
  33. reaper14

    reaper14

    Joined:
    Apr 9, 2015
    Posts:
    5
    Screen Shot 2015-05-31 at 16.03.54.png hi I've been doing the tutorials for survival shooter and i am on level 2 which is sorting out the player movement but the bit i am stuck on is all the code has been inserted and i should be able to control the character with the direction keys but it doesn't work and comes up with an error message which states "null reference exception, object reference not set to an instance of an object"

    Screen Shot 2015-05-31 at 16.04.10.png
     
  34. kyleunruh

    kyleunruh

    Joined:
    May 11, 2015
    Posts:
    17
    Hi @reaper14. I can't see the top part of your script, but that is likely where the problem is. In your variables at the top of the script, you should have a declaration for the player rigidbody and then you should be getting the Rigidbody component and setting it to that variable in the Awake function, similar to this:
    Code (CSharp):
    1.  
    2. //other variables....
    3. Rigidbody playerRigidBody;
    4.  
    5. Awake()
    6. {
    7.      //other code....
    8.      playerRigidbody = GetComponent<Rigidbody>();
    9. }
    Make sure your variable name (in the example above it is playerRigidBody) is exactly the same as your calls to it later on in the script. It could be that you set it up right, but just accidently capitalized the 'P' on playerRigidBody. Case syntax is pretty important in programming languages and you almost always want variable names to start with a lowercase letter.

    Also, keep in mind if you every have errors in your script, you can compare yours with the completed scripts that are at the bottom of every tutorial video page, like this one: https://unity3d.com/learn/tutorials/projects/survival-shooter/player-character
     
  35. reaper14

    reaper14

    Joined:
    Apr 9, 2015
    Posts:
    5


    the whole script:
    usingUnityEngine;

    publicclassPlayerMovement : MonoBehaviour
    {
    publicfloatspeed = 6f;
    Vector3movement;
    Animatoranim;
    RigidbodyPlayerRigidbody;
    intfloorMask;
    floatcamRayLength = 100f;

    voidawake()
    {
    floorMask = LayerMask.GetMask ("floor");
    anim = GetComponent<Animator> ();
    PlayerRigidbody = GetComponent <Rigidbody> ();

    }
    voidFixedUpdate()
    {
    floath = Input.GetAxisRaw ("Horizontal");
    floatv = Input.GetAxisRaw ("Vertical");

    Move (h, v);
    turning ();
    Animating (h, v);


    }

    voidMove (floath, floatv)
    {
    movement.Set (h, 0f, v);

    movement = movement.normalized * speed * Time.deltaTime;
    PlayerRigidbody.MovePosition (transform.position + movement);

    }

    voidturning()
    {
    RaycamRay = Camera.main.ScreenPointToRay (Input.mousePosition);
    RaycastHitfloorHit;
    if (Physics.Raycast (camRay, outfloorHit, camRayLength, floorMask))
    {
    Vector3playerToMouse = floorHit.point - transform.position;
    playerToMouse.y = 0f;

    QuaternionnewRotation = Quaternion.LookRotation (playerToMouse);
    PlayerRigidbody.MoveRotation (newRotation);

    }
    }

    voidAnimating(floath, floatv)
    {
    boolwalking = h != 0f || v != 0f;
    anim.SetBool ("IsWalking", walking);
    }
    }
     
  36. kyleunruh

    kyleunruh

    Joined:
    May 11, 2015
    Posts:
    17
    That line is where the problem is: You need a space after Rigidbody. Also, after adding the space, be sure to make the 'P' on Player lowercase. It should look like this:
    Code (CSharp):
    1. Rigidbody playerRigidBody;
    Rigidbody is the Type and playerRigidbody is just your variable name. Make sure all the other places where you use that variable have a lowercase 'p' as well!

    EDIT: Nevermind, I just noticed a lot of the spaces got removed when you pasted your script so I'm assuming they are actually there. That being the case, the problem is probably still your naming convention. Change the 'P' on "PlayerRigidbody" to lowercase and see if that helps.
     
  37. kyleunruh

    kyleunruh

    Joined:
    May 11, 2015
    Posts:
    17
    Oh, noticed one more thing: make sure you capitalize the Awake function. That is a built-in Unity function and it is case sensitive.
     
  38. reaper14

    reaper14

    Joined:
    Apr 9, 2015
    Posts:
    5
    Your an absolute legend. i changed all the rigid body tags to lowercase "p" and then changed the awake function to a capital and its working.
     
    kyleunruh likes this.
  39. reaper14

    reaper14

    Joined:
    Apr 9, 2015
    Posts:
    5
    just got a quick question, the character is now moving but only i straight lines and comparing to the tutorial by using the mouse the character should be able to look around?
     
  40. Wabefuhon

    Wabefuhon

    Joined:
    May 27, 2015
    Posts:
    13
    I had this issue, look further up and you'll see the discussion. Check to see if it's the floor.
     
    kyleunruh likes this.
  41. reaper14

    reaper14

    Joined:
    Apr 9, 2015
    Posts:
    5
    hi, I've deleted the floor component and re added it but the same problem still occurs. the player runs only in straight lines and doesn't respond to the mouse
     
  42. Wabefuhon

    Wabefuhon

    Joined:
    May 27, 2015
    Posts:
    13
    Check further back.
    Make sure you Main Camera is Tagged. I'm referencing kyleunruh.

    When in doubt, go back to the beginning where I asked the question and go over everything. Double check your work and make sure that you're following Tutorial 2.

    On another note, I have a bullet problem.

    Where the line representing the bullet is suppose to render, it's not. All I'm getting is a black line.

    I do have GunBarrelEnd applied. I do have Line Render Material included. I don't know what the problem is.
     
  43. kyleunruh

    kyleunruh

    Joined:
    May 11, 2015
    Posts:
    17
    Hi Wabefuhon, peteorstrike mentioned the fix for this here: http://forum.unity3d.com/threads/survival-shooter-q-a.313934/#post-2059232
     
  44. kyleunruh

    kyleunruh

    Joined:
    May 11, 2015
    Posts:
    17
    Oh and you can find the Shader setting for the material by following this image:

    line_renderer_mat_settings.PNG

    (I actually used a different shader than peteorstrike suggested but they both look pretty much the same)
     
  45. Adam-Buckner

    Adam-Buckner

    Joined:
    Jun 27, 2007
    Posts:
    5,664
    PLEASE! Learn to format your code:
    http://forum.unity3d.com/threads/using-code-tags-properly.143875/
     
  46. Wabefuhon

    Wabefuhon

    Joined:
    May 27, 2015
    Posts:
    13
  47. scorpionz

    scorpionz

    Joined:
    Oct 11, 2014
    Posts:
    3
    I am following the survival shooter tutorial in Unity 5...
    Currently I am in the sixth video and having some problem with the Enemy movement script. The position of the player doesn't update. It stays at origin co-ordinates... The enemy goes to the origin and then stop moving.
    What is going wrong here??

    Code (CSharp):
    1. using UnityEngine;
    2. using System.Collections;
    3.  
    4. public class EnemyMovement : MonoBehaviour
    5. {
    6.     Transform player;
    7.     //PlayerHealth playerHealth;
    8.     //EnemyHealth enemyHealth;
    9.     NavMeshAgent nav;
    10.     GameObject playerObject;
    11.  
    12.  
    13.     void Awake ()
    14.     {
    15.        
    16.         player = GameObject.FindGameObjectWithTag ("Player").transform;
    17.      
    18.         nav = GetComponent <NavMeshAgent> ();
    19.     }
    20.  
    21.  
    22.     void Update ()
    23.     {
    24.  
    25.         Debug.Log ("Player position: " + player.position.x + ", " + player.position.y + "," + player.position.z);
    26.  
    27.             nav.SetDestination (player.position);
    28.    
    29.     }
    30. }
    31.  
     
  48. kyleunruh

    kyleunruh

    Joined:
    May 11, 2015
    Posts:
    17
    Hi @scorpionz! Are you seeing any errors or warnings at the bottom of the unity window? Your code looks fine to me, so something in the settings must have not gotten set correctly. My first guess would be: check to make sure your Player game object has it's Tag set to "Player".

    (Here's an example, however this is for the Main Camera, so be sure you have the Player object selected)
     
  49. scorpionz

    scorpionz

    Joined:
    Oct 11, 2014
    Posts:
    3
    @kyleunruh Hi, thanks for your help... I rechecked everything and found, though the tag was set for the player object in Hierarchy panel I found that in the player prefab it wasn't set to "Player". So I set it for the prefab and it worked. But it should have worked even with the tag set in only hierarchy panel, right?
     
  50. Adam-Buckner

    Adam-Buckner

    Joined:
    Jun 27, 2007
    Posts:
    5,664
    This would depend upon how the game is structured. I can't remember the details as this is not one of mine, but if the game is instantiating a prefab, it won't work as expected.
     
Thread Status:
Not open for further replies.