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 Colajojo · Jun 06, 2014 at 02:48 PM · bugsrpg-gamegeneral

RPG Mouse targeting error

Hi! Need help. Im making a rpg and i made a C# script. So i can mark which npc i clicked on. But the problem is the diselect thing.

Code:

 using UnityEngine;
 using System.Collections;
 using System.Collections.Generic;
 
 public class NpcTargeting : MonoBehaviour 
 {
 
     public List<Transform> targets = new List<Transform>();
     public Transform selectedTarget;
     public GameObject myTransform;
 
     public RaycastHit hit = new RaycastHit();
     public Ray ray;
 
     // Use this for initialization
     void Start () 
     {
         selectedTarget = null;
     }
     
     // Update is called once per frame
     void Update () 
     {
         
         if (Input.GetKeyDown(KeyCode.Mouse0))
         {
             ray = Camera.main.ScreenPointToRay(Input.mousePosition);
             if (Physics.Raycast(ray, out hit))
             {
                 selectedTarget = hit.transform;
                 targets.Add(selectedTarget);
                 myTransform = hit.transform.gameObject;
             }
         }
 
         if(selectedTarget != null)
         {
             selectedTarget.Find("Graphic/NpcCircle").gameObject.SetActive(true);
             
         }
         if(selectedTarget == null)
         {
             if(targets.Count == 0)
             {
                 myTransform.transform.Find("Graphic/NpcCircle").gameObject.SetActive(false);
             }
         }
         if(targets.Count == 2)
         {
             
             targets.Clear();
             myTransform.transform.Find("Graphic/NpcCircle").gameObject.SetActive(false);
             selectedTarget = null;
         }
        
 
     }
 }

I'll get an error where it says "UnassignedReferenceException: The variable myTransform of NpcTargeting has not been assigned."

Comment
Add comment
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

1 Reply

· Add your reply
  • Sort: 
avatar image
0

Answer by Jeff-Kesselman · Jun 06, 2014 at 02:49 PM

You can figure this out yourself.

What does your code do when Physics.Raycast returns false. What value does myTransform have? What value does SelectedTarget have?

Walk it through line by line and you will see it.

Comment
Add comment · Show 3 · 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 Colajojo · Jun 06, 2014 at 03:29 PM 0
Share

what do you mean? i want to get rid off the error

avatar image Landern · Jun 06, 2014 at 04:08 PM 0
Share

What Jeff was is trying to convey is that learning to properly debug your code will be a wonderful thing to add to your $$anonymous$$d toolkit.

myTransform is declared but not initialized as a public field, its default value is null. When you try and use your ray cast hit but it's hit was false, myTransform is not set, yet below there is usage of the variable. Jeff is asking you to think about the condition of when myTransform is never set to a reference of an object... leading to your exact exception.

avatar image Colajojo · Jun 06, 2014 at 04:39 PM 0
Share

ok, i declared "myTransform" as null, in the "Start" function. now it gives me a "NullReferenceException: Object reference not set to an instance of an object" error? srry if im little retarded, im new to c# program$$anonymous$$g(1 month).

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

23 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

Related Questions

Error CS0029 1 Answer

I'm Getting Weird Bugs Today 1 Answer

FBX Scale Wobble/Shake problem 0 Answers

Assetbundle sample is not working in unity4.6.1(WP8) 0 Answers

Issues concerning the sample projects 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