Ok so i have gone up and down this script trying to find out what is causing this error. i cant find the cause anywhere. the error is being caused on line 114 and 112 in my code. but it does not specipy what object reference is not being instantiated.
Your “hit” RaycastHit object is never being assigned. Try using Physics.Raycast(ray, out hit, 10) instead.
lol@posting 4 pages of screenshots…
Please post code as actual code…http://forum.unity3d.com/threads/143875-Using-code-tags-properly
–Eric
ummm, this is actual code…
i did that and it still did not work
go to the thread
I believe you need to access hit.collider.gameObject.tag at line 112 - you’ve skipped the “collider” part.
If that doesn’t work, your issue may be more related to the ray not hitting anything - are you sure it’s casting in the proper direction, and of adequate length? It seems like your issue is that whatever object you’re trying to check the tag of doesn’t exist, which is indicative of either code trying to look in the wrong place (skipping the collider part) or else code looking in the right place for an object that isn’t there (hit not being assigned).
changed it to if(hit.collider.gameObject.tag == “Ablock”) that also did not work. Now i’m getting the error:
NullReferenceException: Object reference not set to an instance of an object
RandomWordGeneratro.Update () (at Assets/MyScripts/RandomWordGeneratro.cs:112)
using UnityEngine;
using System.Collections;
public class RandomWordGeneratro : MonoBehaviour
{
//======================================================================
#region Declariations
bool isAppleWordComplete = false;
bool isOrangeWordComplete = false;
bool isPearWordComplete = false;
bool isDestroyed = false;
string appleString = "Apple";
enum GameState { spellApple, spellOrange, spellPear }
GameState currentState;
public Texture2D[] lettersArray;
public Texture2D[] wordsArray;
#endregion
//======================================================================
#region OnGUI
void OnGUI()
{
if( currentState == GameState.spellApple)
{
//Rect (left, top, width, height)
GUI.DrawTexture(new Rect(10, 10, 200, 100), wordsArray[0]);
}
else if( currentState == GameState.spellOrange)
{
//Rect (left, top, width, height)
GUI.DrawTexture(new Rect(10, 10, 200, 100), wordsArray[1]);
}
else
{
//Rect (left, top, width, height)
GUI.DrawTexture(new Rect(10, 10, 200, 100), wordsArray[2]);
}
}
#endregion
//======================================================================
#region Initilize (AKA Start)
// Use this for initialization
void Start ()
{
int rnd = Random.Range(0, 3); // Generate a random number to choose what state to be in
if(rnd == 0)
{
currentState = GameState.spellApple;
Debug.Log (" In spellApple State ");
}
else if(rnd == 1)
{
currentState = GameState.spellOrange;
Debug.Log (" In spellOrange State ");
}
else if(rnd == 2)
{
currentState = GameState.spellPear;
Debug.Log (" In spellPear State ");
}
}
#endregion
//======================================================================
#region Update
// Update is called once per frame
void Update ()
{
//======================================================================
#region GameStateApple
switch (currentState)
{
case GameState.spellApple:
if(Input.GetMouseButton(0))
{
Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);
RaycastHit hit;
if(Physics.Raycast(ray, 10))
{
// Conditionals for Hitting the proper blocks
if(hit.collider.gameObject.tag == "Ablock")
{
if( appleString.Contains("A") gameObject.tag == "Ablock" )
{
Destroy(hit.transform.gameObject);
GUI.DrawTexture(new Rect(10, 50, 200, 100), lettersArray[0]);
isDestroyed = true;
appleString = appleString.Remove (0,1);
Debug.Log(appleString);
if(isDestroyed == true)
{
isAppleWordComplete = true;
}
}
}
}
}
// when all the blocks are destroyed complete the word and change the state
if(isAppleWordComplete == true)
{
currentState = GameState.spellOrange;
}
break;
#endregion
//======================================================================
#region GameStateOrange
case GameState.spellOrange:
if(isOrangeWordComplete == true)
{
currentState = GameState.spellPear;
}
break;
#endregion
//======================================================================
#region GameStatePear
case GameState.spellPear:
if(isPearWordComplete == true)
{
currentState = GameState.spellApple;
}
break;
}
}
#endregion
//======================================================================
#endregion
//======================================================================
#region EndBracde
}
#endregion
//======================================================================
I would suggest waiting until you’re better at scripting yourself before ‘helping’.
-
Original code using transform.gameObject.tag was correct.
-
The line of code in question wont be hit unless the raycast hits something
With assigning hit, I cant see anything wrong with the code. Perhaps you could post an updated version of your code correctly (NOT SCREENSHOTS!!)
use [ code ] [ / code ] tags (remove the extra spaces)
Also, not sure if this is causing the issue, but the block in the image below are generated with a spawn gameObject. the script is turned on when the state is selected. then spawns the objects. the prefabs that are being generated are tagged correctly.
The issue is that you’re ray casting, but you’re not passing your raycasthit into the ray cast.
Change line 108 to:
if(Physics.Raycast(ray, out hit, 10)){
lol… so still hadnt made the one change that was needed… fail
Ummm, let me see, i have used unity for about a week and i fail at the first script i write. How old are you?? Grow Up Dude!!!
i will give that a try
That fixed the null error, but my objects are still not being destroyed
His reply wasn’t about your Unity knowledge, it was a (rather rude imo) way of pointing out that the solution was given to you in the very first reply of this thread. If flaminghairball hadn’t brought it up again, you may have been running in circles for a while
Either way, fighting hostility with more hostility is never a way to get things done. Especially on the internet.
EDIT:
Are any of your logs firing? If not, add more logs so you can better understand what’s happening (is the raycast hitting anything, if so what? etc)
Your right, sorry to fight on a forum. i did try that on the first post and it did not work. so i must have fixed another error in my code that ended up making that work.
I am doing that log thing right now actually. there are not flags going off, so i have a bug burred in the code somewhere.
ok, so i have been trying to debug this for hours now, and i cant seem to find out why when the Ray hits my object that it does not destroy it when the args are met