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 /
avatar image
0
Question by psraganvesh · May 02, 2016 at 05:13 AM · c#errornullreferenceexception

I cannot understand where the Object reference is not set to an instance of an object?

NullReferenceException: Object reference not set to an instance of an object TouchHandler.TouchControl () (at Assets/Script/TouchHandler.cs:78) TouchHandler.Update () (at Assets/Script/TouchHandler.cs:39)

I get this error and i cant figure out why. I thought some fresh eyes might help, anyone? Thank You.

using UnityEngine; using System.Collections;

public class TouchHandler : MonoBehaviour {

 /*****All the touch variables******/
 private Vector2     fp;                 // first finger position
 private Vector2     lp;                 // last finger position
 private float       angle;            
 private float       swipeDistanceX;
 private float       swipeDistanceY;
 private int         swipeDistance = 50;    // Distance fingure to travell to register as a swipe
 private Touch       touch;            // touch variable        



 /*****All the Raycast variables******/
 Ray ray;
 RaycastHit hitInfo = new RaycastHit();

 /*****All the PlayerController script variables******/
 private PlayerController PC_component;


 //private MovementHandler movementHandlerScriptComponent;


 void Start()
 {
     PC_component = GetComponent<PlayerController>();

     //movementHandlerScriptComponent = GetComponent<MovementHandler>();
     //movementHandlerScriptComponent.SetisBaseNameSet(false);
 }

 void Update () 
 {
     TouchControl();
 }

 void OnGUI()
 {
     foreach(Touch t in Input.touches)
     {
         string message = "";
         
         message += "ID: "         + t.fingerId             + "\n"; 
         message += "Phase: "     + t.phase.ToString()     + "\n";
         message += "TapCount: " + t.tapCount             + "\n";
         message += "X: "         + t.position.x             + "\n";
         message += "Y: "         + t.position.y             + "\n";
         message += "Delta: "     + t.deltaPosition         + "\n";
         int num = t.fingerId;
         
         GUI.Label(new Rect(0 + 130 * num, 0, 120, 120), message);
         
     }
 }
 void castingRay()
 {
     if(Physics.Raycast(ray,out hitInfo))
     {
         if(hitInfo.transform.tag == "Base")
         {
             PC_component.spawnPlayer(hitInfo);

             //movementHandlerScriptComponent.Setsb_(hitInfo);
             //movementHandlerScriptComponent.SetisBaseNameSet(true);
         }
     }
 }
 void TouchControl()
 {
     if(Input.touchCount == 1)
     {
         touch = Input.GetTouch(0);
         ray = Camera.main.ScreenPointToRay(touch.position);
         
         if (touch.phase == TouchPhase.Began)
         {
             fp = touch.position;
             lp = touch.position;
             
             //Raycasting
             //if(movementHandlerScriptComponent.GetisBaseNameSet() == false)
             if(PC_component.isPlayerSpawned == false)
                 castingRay();
         }
         if (touch.phase == TouchPhase.Moved )
         {
             lp = touch.position;
             swipeDistanceX = Mathf.Abs((lp.x-fp.x));
             swipeDistanceY = Mathf.Abs((lp.y-fp.y));
         }
         if(touch.phase == TouchPhase.Ended)
         {
             angle = Mathf.Atan2((lp.x-fp.x),(lp.y-fp.y))*57.2957795f;
             swipeControlls();
         }
     }
 }

 void swipeControlls()
 {
     if(angle > 60 && angle < 120 && swipeDistanceX > swipeDistance)
     {
         Debug.Log("right");
         PC_component.moveDirection = "right";
     }
     if(angle > 150 || angle < -150 && swipeDistanceY > swipeDistance)
     {
         Debug.Log("down");
     }
     if(angle < -60 && angle > -120 && swipeDistanceX > swipeDistance)
     {
         Debug.Log("left");
         PC_component.moveDirection = "left";
     }
     if(angle > -30 && angle < 30 && swipeDistanceY > swipeDistance)
     {
         Debug.Log("up");
     }
 }



}

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 UsmanAbbasi · May 02, 2016 at 10:05 AM 1
Share

$$anonymous$$aybe the problem is with this line:

  ray = Camera.main.ScreenPointToRay(touch.position);

$$anonymous$$ake sure you have camera in scene which is tagged as "$$anonymous$$ainCamera".

avatar image Ali-hatem · May 02, 2016 at 11:36 AM 0
Share

first : to which game object PlayerController script is attached to the same that above script attached to or else

avatar image Outliver · May 02, 2016 at 02:10 PM 0
Share

Did you

 Debug.Log( PC_component );

?

0 Replies

· Add your reply
  • Sort: 

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

6 People are following this question.

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

Related Questions

NullReferenceException and m_InstanceID == 0 on LoadLevel (C#) 1 Answer

Can't find source of trigger Null Reference Exception 1 Answer

NullReferenceException - When destroying Object 1 Answer

NullReferenceException 1 Answer

Unknown reason for NullReferenceException error 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