Question by
01jsansom · Apr 26, 2016 at 04:23 AM ·
scripting probleminstantiate
Object Reference Not Set to an Instance of an Object?
So I have two points, I need to instantiate an object between them when a trigger is entered. Here are my two scripts.
using UnityEngine;
using System.Collections;
public class BallMove : MonoBehaviour
{
public float speed = 1f;
private int dir;
private Intersections inters;
private CreateObstacle createObstacle;
void Start()
{
createObstacle = GetComponent<CreateObstacle>();
}
void OnTriggerEnter(Collider c)
{
if(c.tag == "Intersection")
{
inters = c.GetComponent<Intersections>();
dir = Random.Range(0, inters.intersections.Length);
createObstacle.InstantiateObstacle(current: inters.transform, next: inters.intersections[dir].transform);
}
}
void Update()
{
if(this.transform.position != inters.intersections[dir].position)
{
this.transform.position = Vector3.MoveTowards(this.transform.position, inters.intersections[dir].position, speed * Time.deltaTime);
}
}
}
And the one with the function to create the object.
using UnityEngine;
using System.Collections;
public class CreateObstacle : MonoBehaviour
{
public GameObject obstacle;
private Vector3 middlePoint;
public void InstantiateObstacle(Transform current, Transform next)
{
middlePoint = Vector3.Lerp(current.position, next.position, 0.5f);
Instantiate(obstacle, middlePoint, Quaternion.identity);
}
}
I really don't know what I'm doing wrong.
Comment
Did you put any gameobject for "obstacle" in the editor. Othervise it is null.
Your answer
![](https://koobas.hobune.stream/wayback/20220612075625im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
Assign variables on Instantiation? 0 Answers
Add Component with Script Data 1 Answer
How to change the Bool for only a single prefab GameObject creating with Instantiate? 2 Answers
Is there a way to edit an uninstantiated object? 1 Answer
Instantiating object randomly withing the boundaries of an object. 0 Answers