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 /
This question was closed Mar 26, 2017 at 08:38 AM by Nekoluffy for the following reason:

The question is answered, right answer was accepted

avatar image
1
Question by Nekoluffy · Mar 28, 2017 at 07:08 AM · c#unity 5null reference exception

null reference but component shows up in the inspector.

I am getting null reference exception

when debugging. It returns the component and then after says it is null. I ran it in update and it just goes back and forth between the component and null

[[url=https://postimg.org/image/rpvku7z29/][img]https://s16.postimg.org/rpvku7z29/cap2.png[/img][/url]

https://s16.postimg.org/yhlzx2o1x/Capture.png ][1] << link to images to show you what im getting. Its saying that the playermove is null. I debugged it. and even initialized it at the point of the error if it returned null. it keeps saying its null and not working. the variables inside the script are public as well. alt text

 public class PlayerMeleeManager : MonoBehaviour {
     
     public PlayerMovement playerMove;
     public PlayerManager playerManager;
 
     int maxCombo;
     public int attIndex;
     int attackState;
 
     bool targetHit;
     public bool lockedOn;
     bool comboTriggered;
     public bool attacking = false;
 
 
     private void Start()
     {
         if (GetComponent<PlayerMovement>())
         {
             playerMove = GetComponent<PlayerMovement>();
         }
         else
         {
             Debug.Log("cant find it");
         }
 
 
         playerManager = GetComponent<PlayerManager>();
         InitializeAttacks();
         InitializeVaraibles();
     }
 
     private void Update()
     {
         CheckForInput();
     }
 
 
 
 
 
     void InitializeVaraibles()
     {
         //maxCombo = AttackListManager.meleeAttackOrder.Count;
         attacking = false;
         targetHit = false;
         comboTriggered = false;
         
     }
 
 
     void CheckForInput()
     {
         
         if (Input.GetKeyDown(KeyCode.Z))
         {
             Attack();
         }
 
     }
 
     void InitializeAttacks()
     {
         AttackListManager.InitializeAbilities();
         AttackListManager.RegulateAttacks(AttackListManager.meleeAttackOrder, AttackListManager.regularGroundComboList);
     }
     
     public void Attack()
     {
         if (!attacking)
         {
             playerMove.playerState = PlayerMovement.PlayerAnimationState.ATTACKING;
             attIndex = 0;
             attacking = true;
         }
         else if (attacking && targetHit && !comboTriggered)
         {
             comboTriggered = true;
         }
         
     }
 
     public void ResetAttack()
     {
         if (comboTriggered)
         {
             targetHit = false;
             comboTriggered = false;
             attIndex++;
         }
         else
         {
             playerMove.playerState = PlayerMovement.PlayerAnimationState.IDLE;
             attIndex = 0;
             attacking = false;
         }
     }
 
 
     
     public void ToggleDamage(bool isOn)
     {
         playerManager.playerWeapon.GetComponent<CapsuleCollider>().enabled = isOn;
 
         if (!isOn)
         {
 
         }
     }
 
     public void StepForward(float x)
     {
         if (lockedOn)
         {
             playerManager.playerRigidbody.AddForce(Vector3.zero, ForceMode.VelocityChange);
             playerManager.playerRigidbody.AddForce(transform.forward * x * 3, ForceMode.Impulse);
         }
         else
         {
             Vector3 dir = PlayerTargetManager.playerTarget.position - transform.position;
             dir = dir.normalized;
             playerManager.playerRigidbody.AddForce(Vector3.zero, ForceMode.VelocityChange);
             playerManager.playerRigidbody.AddForce(dir * x * 3, ForceMode.Impulse);
         }
     }
 }
 


capture.png (19.4 kB)
cap2.png (24.3 kB)
Comment
Add comment · Show 1
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 svendkiloo · Dec 18, 2018 at 10:25 AM 0
Share

Why does it say this has been closed with an accepted answer? Doesn't seem to have any answers at all, and seems like a quite valid question. I'm experiencing the same where I'm setting a reference on a newly spawned gameobject's component through an Init method. The reference is visible in the inspector, but when accessing it through script I get a null reference. Probably the same issue?

0 Replies

  • Sort: 

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

Object reference is NULL when IT IS set to an instance of an object? 1 Answer

Multiple Cars not working 1 Answer

Distribute terrain in zones 3 Answers

Illuminating a 3D object's edges OnMouseOver (script in c#)? 1 Answer

The type or namespace name `Events' does not exist in the namespace `TouchScript' 1 Answer


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