My code is supposed to instantiate chosenMicroObject into the neuralNet object. But for some reason the game object (temp) wont change position or get neuralNet as parent… I don’t understand the error:
“InvalidCastException: Cannot cast from source type to destination type.
mouseButtonActions.Update () (at Assets/scripts/mouseButtonActions.cs:26)”
I have googled it and searched here but noone seems to have the same problem.
The important parts are with stars code
Here is the code:
using UnityEngine;
using System.Collections;
public class mouseButtonActions : MonoBehaviour {
public float tehFloat = 1.0f;
public Transform[] microObjects;
public static int chosenMicroObject = 0;
**public GameObject neuralNet;**
// Use this for initialization
void Start () {
}
// Update is called once per frame
void Update () {
if( Input.GetMouseButtonDown(0) )
{
Ray ray = Camera.main.ScreenPointToRay( Input.mousePosition );
RaycastHit hit;
if( Physics.Raycast( ray, out hit, 100 ) )
{
**GameObject temp = (GameObject)Instantiate(microObjects[chosenMicroObject], hit.transform.gameObject.transform.position, Quaternion.identity);**
**temp.transform.position = new Vector3(0,0,0);**
**temp.transform.parent = neuralNet.transform;**
if (temp)
{
Debug.Log("temp is");
}
else
{
Debug.Log("temp is not");
}
}
}
}
}