Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 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 /
avatar image
0
Question by Dawnk41 · Apr 24, 2014 at 03:14 AM · object-reference-error

"Object Reference not set to an instance of an object" What does it mean?

Alright, so I have this bit of code set up, which helps my character attack things. When I press the "A" key, and left click on the screen, I set my character to attack that point on the screen. Everything goes well up until he gets within attack range. The attack instance, attackPrefab, does not actually appear where it should. Here is my code:

 using UnityEngine;
 using System.Collections;
 
 public class gladiator_combat_script : MonoBehaviour
 {
     public GameObject character;
     private gladiator_base_stats stats;
     bool attackingClick = false;
     public bool attacking = false;
     float targetDistance;
     Vector3 attackTrue;
     public GameObject attackPrefab;
     public GameObject movePoint;
 
     void Update()
     {
         if(Input.GetKey("a"))
         {
             attackingClick = true;
         }
 
         if (Input.GetMouseButtonDown (0))
         if(attackingClick == true)
         {
             RaycastHit attackPoint;
             Vector3 attackPos = Input.mousePosition;
             attackPos.z = 25f;
             Debug.Log(Camera.main.ScreenToWorldPoint (attackPos));
             attackTrue = Camera.main.ScreenToWorldPoint (attackPos);
             Physics.Raycast (Camera.main.transform.position,attackTrue, out attackPoint);
             Debug.Log (attackPoint);
             movePoint.transform.position = new Vector3(attackTrue.x, -0.45f, attackTrue.z);
             attacking = true;
         }
 
         if (attacking == true)
         {
             targetDistance = Vector3.Distance(transform.position,attackTrue);
             Debug.Log (targetDistance);
             if (targetDistance < stats.attackRange)
             {
                 if (stats.attackCooldown == 0f)
                 {
                 Rigidbody attackInstance;
                 stats.attackCooldown = Time.deltaTime * 1;
                 attackInstance = Instantiate(attackPrefab, attackTrue, character.transform.rotation) as Rigidbody;
                 }
             }
         }
     }

Which means this line: "if (targetDistance < stats.attackRange)" should be the source of error. Does anyone know how to make the attackPrefab actually GET created? Thanks!

Comment
Add comment · Show 3
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 EvilTak · Apr 24, 2014 at 07:29 AM 0
Share

Check if you have assigned attackPrefab in the inspector before running.

avatar image teslatron · Apr 24, 2014 at 08:08 AM 0
Share

I don't see you initialize "stats" anywhere in the code. If stats is null, you will get exception on stats.attackRange.

avatar image EvilTak · Apr 24, 2014 at 08:23 AM 0
Share

Yes, you may want to initialize it in Start() or Awake() using GetComponent() function.

2 Replies

· Add your reply
  • Sort: 
avatar image
0

Answer by trex1121@aol.com · Apr 24, 2014 at 09:25 AM

Same info as everyone else. You are using stats (which is a reference/instance of gladiator_base_stats) but you have not fully declared it as an instance before using it. You are currently using it as if it is a value type.,

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
avatar image
0

Answer by Ianson · Apr 24, 2014 at 10:54 AM

As other have said you don't actually instantiate (read "create") stats so when you try and reference it it isn't there.

Depending on where the stats are supposed to come from you have a few options:

A: If you only want to check attack range after some other part of your code has instantiated and initialised gladiator_combat_script.stats then wrap the offending code in a null check:

 if( this.stats != null )
 {
 /// do stuff
 }

This could include making it public and assigning it through the editor, IF it is a monobehaviour...

B: as others have say you simply need to instantiate stats: // Either do it when you declare it private gladiator_base_stats stats = new gladiator_base_stats();

 // Or, as other have said do it in start
 void Start()
 {
      stats = new gladiator_base_stats();
 // do any initialisations 
 }
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.

Follow this Question

Answers Answers and Comments

24 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

Related Questions

Can't assign materials to objects from an array 3 Answers

Object reference not set to an instance of an object... 0 Answers

Object reference not set to an instance of an object in c# class. 1 Answer

NullReferenceException: Object reference not set to an instance of an object Admanager.ShowFullScreenAd () 0 Answers

weird NullReferenceException 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