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 Ochreous · Dec 08, 2014 at 05:04 AM · c#nullreferenceexception

C# Need Help Finding Where Null Reference Exception Is

I have a script that generates a rope down the y axis. For some reason I'm getting a null reference exception and I can't figure out where it's coming from. When I click on it sends me to random lines on my code. Any help would be appreciated.

 using UnityEngine;
 using System.Collections;
 using System.Collections.Generic;
 public class RopeController : MonoBehaviour
 {
     public GameObject fragmentPrefab;
     
     public  List<GameObject> fragments = new List<GameObject>();
     private Vector3 interval = new Vector3(0f, -0.5f, 0f);
 
     private float[] xPositions;
     private float[] yPositions;
     private float[] zPositions;
     
     private CatmullRomSpline splineX;
     private CatmullRomSpline splineY;
     private CatmullRomSpline splineZ;
     
     private int splineFactor = 4;
     
     void Start()
     {
 
     }
     
     void Update()
     {
         Vector3 position = transform.position;
         
         for (int i = 0; i < (int)gameObject.transform.position.y; i++) {
         if(fragments.Count <= (int)gameObject.transform.position.y){
             fragments.Add(null);
             fragments[i] = (GameObject) Instantiate(fragmentPrefab, position, Quaternion.identity);
             fragments[i].transform.parent = transform;
             
             SpringJoint joint = fragments[i].GetComponent<SpringJoint>();
             if (i > 0) {
                 joint.connectedBody = fragments[i - 1].rigidbody;
             }
             
             position += interval;
             }
         }
         
         LineRenderer renderer = GetComponent<LineRenderer>();
         renderer.SetVertexCount((fragments.Count - 1) * splineFactor + 1);
         
         xPositions = new float[fragments.Count];
         yPositions = new float[fragments.Count];
         zPositions = new float[fragments.Count];
         
         splineX = new CatmullRomSpline(xPositions);
         splineY = new CatmullRomSpline(yPositions);
         splineZ = new CatmullRomSpline(zPositions);
         
         float activeFragmentNum = (float)fragments.Count;
         float vy = Input.GetAxisRaw("Vertical") * 20f * Time.deltaTime;
         activeFragmentNum = Mathf.Clamp(activeFragmentNum + vy, 0, fragments.Count);
         
         for (int i = 0; i < fragments.Count; i++) {
             if (i <= fragments.Count - activeFragmentNum) {
                 fragments[i].rigidbody.position = transform.position;
                 fragments[i].rigidbody.isKinematic = true;
             } else {
                 fragments[i].rigidbody.isKinematic = false;
             }
         }
     }
     
     void LateUpdate()
     {
         LineRenderer renderer = GetComponent<LineRenderer>();
         
         for (int i = 0; i < fragments.Count; i++) {
             Vector3 position = fragments[i].transform.position;
             xPositions[i] = position.x;
             yPositions[i] = position.y;
             zPositions[i] = position.z;
         }
         
         for (int i = 0; i < (fragments.Count - 1) * splineFactor + 1; i++) {
             renderer.SetPosition(i, new Vector3(
                 splineX.GetValue(i / (float) splineFactor), 
                 splineY.GetValue(i / (float) splineFactor), 
                 splineZ.GetValue(i / (float) splineFactor)));
         }
     }
 }
 
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 theLucre · Dec 08, 2014 at 05:59 AM 1
Share

The exception should have a stack trace. Which line is the error co$$anonymous$$g from in your script?

2 Replies

· Add your reply
  • Sort: 
avatar image
0
Best Answer

Answer by zharik86 · Dec 08, 2014 at 07:51 AM

You have error, because your logic in loop "for" is not correct. You write:

  for (int i = 0; i < (int)gameObject.transform.position.y; i++) {
   if(fragments.Count <= (int)gameObject.transform.position.y){
    fragments.Add(null);
     fragments[i] = ...

For, example, List of fragments is not empty (for example, have 5 elements). Then in condition "if" (for i == 0) you add null object. Last object have index 5, but you work with index 0. I think, you must modify last element of List. Try this:

  void Update() {
   Vector3 position = transform.position;
      
   for (int i = 0; i < (int)gameObject.transform.position.y; i++) {
    if(fragments.Count <= (int)gameObject.transform.position.y){
     fragments.Add(null);
     //Use not "i", but last index
     fragments[fragments.Count-1] = (GameObject) Instantiate(fragmentPrefab, position, Quaternion.identity);
     fragments[fragments.Count-1].transform.parent = transform;
     
     SpringJoint joint = fragments[fragments.Count-1].GetComponent<SpringJoint>();
     if (fragments.Count-1 > 0) {
      //Connect to prelast element
      joint.connectedBody = fragments[fragments.Count - 2].rigidbody;
     }
          
     position += interval;
    }
   }

   //Not use names matches with method, change "renderer" on "myrenderer"
   LineRenderer myrenderer = GetComponent<LineRenderer>();
   //Check counts:
   if(fragments.Count > 0) {
    myrenderer.SetVertexCount((fragments.Count - 1) * splineFactor + 1);

    //Not correct index, for example list have 7 elements (Count), but last index is 6
    xPositions = new float[fragments.Count - 1];
    yPositions = new float[fragments.Count - 1];
    zPositions = new float[fragments.Count - 1];
      
    splineX = new CatmullRomSpline(xPositions);
    splineY = new CatmullRomSpline(yPositions);
    splineZ = new CatmullRomSpline(zPositions);
      
    float activeFragmentNum = (float)fragments.Count;
    float vy = Input.GetAxisRaw("Vertical") * 20f * Time.deltaTime;
    activeFragmentNum = Mathf.Clamp(activeFragmentNum + vy, 0, fragments.Count);
      
    for (int i = 0; i < fragments.Count; i++) {
     if (i <= fragments.Count - activeFragmentNum) {
      fragments[i].rigidbody.position = transform.position;
      fragments[i].rigidbody.isKinematic = true;
     } else {
      fragments[i].rigidbody.isKinematic = false;
     }
    }
   }
  }

In LateUpdate() changing name LineRenderer too. I hope that it will help you.

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 Tamir1989 · Dec 08, 2014 at 08:19 AM

you set some parameters as public,(fragmentPrefab...) go to the inspector for that script, check if you have assigned the public parameters.

Tamir.

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

26 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

Related Questions

C# Textfield Null Reference Exception 1 Answer

NullReferenceException After A Few Hours? 1 Answer

C# Null Reference Exception in Custom Function 1 Answer

Working Reference Still Throwing Null Reference Error 0 Answers

C# Another Null reference exception 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