Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. Dismiss Notice

Help variable isn't getting set by Instantiate c#

Discussion in 'Editor & General Support' started by Orcolom_, Dec 6, 2013.

  1. Orcolom_

    Orcolom_

    Joined:
    Jun 28, 2013
    Posts:
    6
    so the problem is that "selection" isn't getting set by the instantiate. and i can't see the problem.
    i'm also getting no errors at all with it, so that doesn't help.

    Anny help would be appreciated. =D

    here you have the script
    Code (csharp):
    1. using UnityEngine;
    2. using System.Collections;
    3. using System.Collections.Generic;
    4.  
    5. public class levelEditor : MonoBehaviour {
    6.     public Transform world;
    7.    
    8.     public myObjectsList list;
    9.     public editorMenu menu;
    10.    
    11.     //cam
    12.     Vector3 nextPosition;
    13.     public float moveSpeed;
    14.  
    15.     //pointer
    16.     public int selectionID = 1;
    17.     public Transform selection;
    18.    
    19.     RaycastHit hit;
    20.    
    21.     void Start(){
    22.         if(list == null)Debug.Log("myObjectsList is empty");
    23.         if(menu == null)Debug.Log("editorMenu is empty");
    24.     }
    25.    
    26.     // Update is called once per frame
    27.     void Update () {
    28.         cam();
    29.        
    30.         if(Input.GetAxis("Mouse ScrollWheel") != 0){
    31.             if(selection != null)Destroy(selection.gameObject);
    32.             changeSelect((int)Input.GetAxis("Mouse ScrollWheel"));
    33.         }
    34.        
    35.         Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);
    36.        
    37.         if(Physics.Raycast(ray, out hit)){
    38.             if(hit.transform.tag == "allow G"){
    39.                 hit.transform.renderer.enabled = false;
    40.                 if(selection != null){
    41.                     selection.position = new Vector3(hit.transform.position.x+1,hit.transform.position.y,hit.transform.position.z+1);
    42.                    
    43.                     if(Input.GetButtonDown("Fire1")){
    44.                         //changeSelect(0);
    45.                         selection = Instantiate(list.objs[selectionID].prefab, new Vector3(0f,0f,0f), Quaternion.identity) as Transform;
    46.                     }
    47.                 }
    48.             }
    49.         }
    50.     }
    51.  
    52.     void changeSelect(int change){
    53.         selectionID += change;
    54.         while(list.objs[selectionID].show = false){
    55.             selectionID += change;
    56.         }
    57.         selection = Instantiate(list.objs[selectionID].prefab, new Vector3(0f,0f,0f), Quaternion.identity) as Transform;
    58.     }
    59.  
    60.     void cam(){
    61.         Vector3 to = transform.position;
    62.         if(Input.GetButton("Horizontal"))to.x += Input.GetAxis("Horizontal")*moveSpeed;
    63.         if(Input.GetButton("Vertical"))to.z += Input.GetAxis("Vertical")*moveSpeed;
    64.         if(Input.GetButton("height movement"))to.y += Input.GetAxis("height movement")*(moveSpeed/2);
    65.         transform.position = to;
    66.     }
    67. }
     
  2. rutter

    rutter

    Joined:
    Mar 21, 2012
    Posts:
    84
    Are you sure Instantiate() is being called? What is list.objs[selectionID].prefab? Is it null? Is it a transform?

    Is your raycast hitting in a way that you expect? ScreenPointToRay() sometimes works better when you pass a vector with non-zero Z.

    In general, check your assumptions.
     
  3. Orcolom_

    Orcolom_

    Joined:
    Jun 28, 2013
    Posts:
    6
    everything works as it should expect the instantiate...
    but i'll double check everything