- Home /
Character Customization Problem, Hard to explain... JS
Hi. So I'm making a character customization system out of boredom, and ran into a few problems. So far the character is a spaceship, or robot or it can be whatever.
It works with each part having points it extends from with more parts. So far I made clicking on a point extend a parts choices tree, and when you click on one of the parts it gets attached and moves to the point. That's all fine.
The problem is even tho its working I'm getting errors once I click on the part I wanna attach. Now I think the problem lays somewhere in my raycasting and assigning some temporary variables, but I can't pinpoint it. Debug isn't helping either.
I have 4 points and when I click on one to make the choice tree appear, Debug runs on all points. And when I click on a part(with part script) I get the error object reference not set to an instance(in point script) but it doesn't exactly tell me what I'm not setting. It just points to "if(hitObject.myName == myName)" in the Point script but I don't get it.
Here's both scripts. I commented on the lines what they're suppose to do if anyone can tell me where I'm making a mistake.
Point
#pragma strict
var side : String;
var myName : String;
var choiceParent : GameObject;
var parts : GameObject[];
var choicePlaces : Transform[];
var hitObjects : GameObject[];
var checkingHit : boolean;
function Update()
{
if(Input.GetMouseButtonDown(0))
{
var hit : RaycastHit;
var ray : Ray = Camera.main.ScreenPointToRay(Input.mousePosition);
if(Physics.Raycast(ray, hit))
{
var hitObject = hit.collider.gameObject.GetComponent(Point);
Debug.Log(hit.collider.gameObject.name); //check which object is hit, all have different myName variables
if(hitObject.myName == myName) //point checking if hit object is me so i do the showing choices stuff
{
checkingHit = true; //just another debug check
Instantiate(choiceParent, transform.position, transform.rotation); //make the parts choice tree
for(var i = 0; i < choicePlaces.Length; i++)
{
var availableChoices = Instantiate(parts[i], choicePlaces[i].position, Quaternion.identity); //instantiate all parts available at their positions
availableChoices.transform.parent = GameObject.FindWithTag("ChoiceParent").transform; //set their parent to the tree
availableChoices.transform.localScale = Vector3(0.1,0.1,0.1); //lower their scale
var availableChoicesScript = availableChoices.gameObject.GetComponent(Part); //get script of those parts
availableChoicesScript.cameFrom = transform; //tell them where they came from
}
}
}
}
}
Part
#pragma strict
var myName : String;
var cameFrom : Transform;
var done : boolean;
function Update()
{
if(Input.GetMouseButtonDown(0))
{
var hit : RaycastHit;
var ray : Ray = Camera.main.ScreenPointToRay(Input.mousePosition);
if(Physics.Raycast(ray, hit))
{
var hitObject = hit.collider.gameObject.GetComponent(Part);
Debug.Log(hitObject.name);
if(hitObject.myName == myName) //if part with my name is clicked on
{
var choiceHolder = transform.parent.gameObject; //assign temp parent var
transform.parent = null; //get rid of parent
Destroy(choiceHolder); //destroy the temp parent var so other choices disappear
transform.position = cameFrom.position; //move to the position where it came from
transform.parent = cameFrom; //become its child
transform.localScale = Vector3(4,4,4); //rescale to normal size
}
}
}
}
Thank you!
Your answer
Follow this Question
Related Questions
Click on GameObject leads to multiple call 1 Answer
Change Camera on (Right) Mouse Click 1 Answer
Problem with OnMouseDown 1 Answer
How do I separate OnMouseDown() from OnMouseDrag() 0 Answers