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 /
  • Help Room /
This question was closed Aug 23, 2016 at 12:06 AM by Brandoboy for the following reason:

The question is answered, right answer was accepted

avatar image
0
Question by Brandoboy · Aug 22, 2016 at 08:35 PM · c#error messagenullreferenceexceptionnull reference exception

Null Reference Exception Error

Hello, I keep getting this error in my 2D C# game:

NullReferenceException: Object reference not set to an instance of an object Character.ThrowKnife (Int32 value) (at Assets/Scripts/Character.cs:43) Player.ThrowKnife (Int32 value) (at Assets/Scripts/Player.cs:171)

here is the Character code:

 public virtual void ThrowKnife(int value)
     {
         if (facingRight)
         {
             GameObject tmp = (GameObject)Instantiate(knifePrefab, KnifePos.position, Quaternion.Euler(new Vector3(0, 0, -90)));
             tmp.GetComponent<Knife>().Initialize(Vector2.right);
         }
         else
         {
             GameObject tmp = (GameObject)Instantiate(knifePrefab, KnifePos.position, Quaternion.Euler(new Vector3(0, 0, 90)));
             tmp.GetComponent<Knife>().Initialize(Vector2.left);
         }
     }

and here is the Player code:

     public override void ThrowKnife(int value)
     {
 
         if (!OnGround && value == 1 || OnGround && value == 0)
         {
             base.ThrowKnife(value);
         }
     }


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 Jessespike · Aug 22, 2016 at 08:39 PM 0
Share

Does knifePrefab have a $$anonymous$$nife script?

 GameObject tmp = (GameObject)Instantiate(knifePrefab, $$anonymous$$nifePos.position, Quaternion.Euler(new Vector3(0, 0, -90)));
 if (tmp.GetComponent<$$anonymous$$nife>() != null) {
     tmp.GetComponent<$$anonymous$$nife>().Initialize(Vector2.right);
 } else {
     Debug.Log("Unable to find $$anonymous$$nife component on tmp");
 }

1 Reply

  • Sort: 
avatar image
0
Best Answer

Answer by Brandoboy · Aug 23, 2016 at 12:00 AM

@Jessespike

yes, there is a knife script,but it still logs "Unable to find knife component on tmp"

here is the knife script:

 using UnityEngine;
 using System.Collections;
 
 [RequireComponent(typeof(Rigidbody2D))]
 
 public class Knife : MonoBehaviour
 {
 
     [SerializeField]
     private float speed;
 
     private Rigidbody2D myRigidbody;
 
     private Vector2 direction;
 
     // Use this for initialization
     void Start () {
         myRigidbody = GetComponent<Rigidbody2D>();
     }
 
     void FixedUpdate()
     {
         myRigidbody.velocity = direction * speed;
     }
 
     public void Initialize(Vector2 direction)
     {
         this.direction = direction;
     }
 
     void OnBecameInvisible()
     {
         Destroy(gameObject);
     }
 
 }
Comment
Add comment · Show 1 · 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 Brandoboy · Aug 23, 2016 at 12:05 AM 0
Share

I fixed it! The $$anonymous$$nifePrefab didn't include the script, so i added it and it worked.

Follow this Question

Answers Answers and Comments

65 People are following this question.

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

Related Questions

GameObject only spawning 60% of the time (c#) 1 Answer

List remains null after initialisation? 1 Answer

Null Reference in UnityStandardAssets.Utility.WaypointProgressTracker.Update 0 Answers

NullReferenceException when attempting to call a GameObject from within PointerEventData 1 Answer

(C#) Invoke giving null reference exception, crashing unity 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