0

I am trying to change this transform.position, for that I first make a vector3 to store the transform.position, make the changes, and then store that Vector3 in another transform. But I keep getting Null Reference Exception

for(int i=0; i<49; i++){
          //aliasboneposition.position = rev_bones[i].position;
          //aliasbonevector = new Vector3(rev_bones[i].position.x,rev_bones[i].position.y,rev_bones[i].position.z) ;
          Vector3 aliasbonevector;
          aliasbonevector = rev_bones[i].position;
          aliasbonevector.x = rev_bones[i+1].position.x;
          aliasboneposition.position = aliasbonevector;
          rev_bones[i+1].position = Vector2.Lerp(rev_bones[i+1].position,aliasboneposition.position, movespeed);
        }
derHugo
  • 83,094
  • 9
  • 75
  • 115
  • 1
    How is the size of your array `rev_bones` ? You can check using `Debug.Log(rev_bones.Length);`. If the size is 49 or higher, this is it, regards to your for loop condition. Plus, you have `i+1` at the very end, you may want to have a condition like `i < rev_bones.Length - 1`. – Malphegal May 12 '20 at 11:42
  • @Malphegal yes rev_bones has a length of 50, and I wanted to iterate over every element except the first, hence the i+1. I changed the 49 to rev_bones.Length-1 but still the Null Reference Exception stays. Now that we know what is causing the problem, could you please tell me how to fix this ? – Aniket Vishwakarma May 12 '20 at 11:45
  • 1
    Where is your NullReferenceException ? Is it `aliasboneposition` ? Is it defined ? And are all array elements defined ? I mean in your for loop `if (rev_bones[i + 1] == null) Debug.Log(i + " is null !");`. – Malphegal May 12 '20 at 12:31
  • 1
    where is the null reference exception? what line? and what is `aliasboneposition`? – Jay May 12 '20 at 12:34
  • @Malphegal the error occurs at : "aliasboneposition.position = aliasbonevector;". Also I checked if any elements were null, and no they weren't. – Aniket Vishwakarma May 12 '20 at 15:38
  • @Malphegal https://codeshare.io/GqmQok --> this is a link to the codeshare – Aniket Vishwakarma May 12 '20 at 15:41
  • @Malphegal I figured it out. aliasboneposition is null. I just had to set it to something before I used it's position. Thank you for your help – Aniket Vishwakarma May 12 '20 at 16:22

1 Answers1

-1

Try adding Transform into your position. Also try putting the Vector3 Variable outside of a void Ex. aliasboneposition.transform.position

Sbfam 22
  • 91
  • 4
  • Try this, Vector3 myVec3Name = neew Vector3(0f, 0f, 0f); – Sbfam 22 May 12 '20 at 11:49
  • Thank you for the suggestion, I tried this but it did nothing sadly. I don't know what the issue is. I have another vector three which works just fine. I set it equal to Input.mousePosition. It's this Vector3 which I am setting equal to Transform.position which is giving trouble – Aniket Vishwakarma May 12 '20 at 11:56
  • Sorry to hear that. I hope you find a solution, probably some dumb mistake you overlooked, I have had those plenty of times. – Sbfam 22 May 13 '20 at 16:07