problem with asset
hi all,
i have a problem with my asset. i imported an .OBJ file to my scene and want to 1. add a script to the imported .obj 2. add physics so it won't fall through my floor.
how do i do that?
Addcomponent<>(); is not working. it is a rigidbody and gets the boxcollider.... it even gets the script (dragviamouse) but it is not dragable (the script is not working for my asset. for a primitve cube it is!!) and it still falls through the floor (primitve cube is not falling through floor!!)
cube.AddComponent<Rigidbody>();
cube.AddComponent<BoxCollider>();
cube.AddComponent<MeshRenderer>();
cube.AddComponent<MeshFilter>();
cube.AddComponent<DragViaMouse>();
Answer by Cuttlas-U · Sep 27, 2017 at 01:55 PM
hi; so u want to do it in runtime ? u said u import the obj file not Instantiate it ? can i see the full script and some screen shots ;
Answer by Pandoflo · Sep 29, 2017 at 04:11 PM
the full script i want to attach to my asset is following:
using UnityEngine;
public class DragViaMouse : MonoBehaviour
{
public Vector3 screenSpace;
public Vector3 offset;
public float speed = 100;
Vector3 dist;
Vector3 startPos;
float posX;
float posZ;
void OnMouseDown()
{
startPos = transform.position;
dist = Camera.main.WorldToScreenPoint(transform.position);
posX = Input.mousePosition.x - dist.x;
posZ = Input.mousePosition.z - dist.z;
}
void OnMouseDrag()
{
float disX = Input.mousePosition.x - posX;
float disY = 0;
float disZ = Input.mousePosition.y - posZ;
Vector3 lastPos = Camera.main.ScreenToWorldPoint(new Vector3(disX, disY, disZ));
transform.position = new Vector3(lastPos.x, startPos.y, lastPos.z);
}
void OnMouseOver()
{
if (Input.GetAxis("Mouse ScrollWheel") > 0)
transform.Rotate(Vector3.up * speed * Time.deltaTime);
if (Input.GetAxis("Mouse ScrollWheel") < 0)
transform.Rotate(-Vector3.up * speed * Time.deltaTime);
if (Input.GetKeyDown("[7]"))
transform.localScale += new Vector3(10f, 0, 0);
if (Input.GetKeyDown("[8]"))
transform.localScale -= new Vector3(10f, 0, 0);
if (Input.GetKeyDown("[4]"))
transform.localScale += new Vector3(0, 10f, 0);
if (Input.GetKeyDown("[5]"))
transform.localScale -= new Vector3(0, 10f, 0);
if (Input.GetKeyDown("[1]"))
transform.localScale += new Vector3(0, 0, 10f);
if (Input.GetKeyDown("[2]"))
transform.localScale -= new Vector3(0, 0, 10f);
}
}
the asset is imported in runtime with this code:#
using System.IO;
using UnityEngine;
public class txtEinlesen : MonoBehaviour
{
StreamReader reader;
private string pth;
private GameObject cubeOne;
private CanvasSampleOpenFileText scriptA;
public void Awake()
{
scriptA = cubeOne.GetComponent<CanvasSampleOpenFileText>();
pth = scriptA.urlreal;
}
public void Start()
{
reader = new StreamReader(pth);
for (pth = reader.ReadLine(); pth != null; pth = reader.ReadLine())
{
var items = pth.Split(' ');
string itemLabel = items[0];
if (items.Length > 1)
{
float x, y, z;
float.TryParse(items[1], out x);
float.TryParse("100", out y);
float.TryParse(items[2], out z);
float x1, y1, z1;
float.TryParse(items[1], out x1);
float.TryParse(items[1], out y1);
float.TryParse(items[1], out z1);
if (itemLabel.Equals("Quelle"))
{
GameObject cube = OBJLoader.LoadOBJFile("O:/PO/Ablage/13_BEI/Florian Pollok/unity/textauslesen/Assets/Resources/1.obj");
cube.transform.position = new Vector3(x, y, z);
cube.transform.name = items[0];
cube.transform.tag = "Plant";
cube.transform.localScale = new Vector3(1, 1, 1);
cube.AddComponent<Rigidbody>();
cube.AddComponent<BoxCollider>();
cube.AddComponent<MeshRenderer>();
cube.AddComponent<MeshFilter>();
cube.AddComponent<DragViaMouse>();
}
else if (itemLabel.Equals("Senke"))
{
GameObject cube = GameObject.CreatePrimitive(PrimitiveType.Cube);
cube.transform.position = new Vector3(x, y, z);
cube.transform.name = items[0];
cube.transform.tag = "Plant";
cube.transform.localScale = new Vector3(100, 100, 100);
cube.AddComponent<Rigidbody>();
cube.AddComponent<DragViaMouse>();
}
}
}
}
}
on the primitive.cube everything works fine. the script works and it is not falling through my floor. the asset on the other side is not doing anything. it gets imported with the right position, name, tag and scale, but any other function is not working. Screenshot would take till tomorrow as i am on my way right now.