I would like to add breakpoint to see the variable’s value when running the C# or JS script.
How can I do that in Unitron?
I would like to add breakpoint to see the variable’s value when running the C# or JS script.
How can I do that in Unitron?
Hi!
You are not alone…
But, sorry to say, there is no such thing at the moment. A debugger is scheduled for Unity 3.0.
In the meantime sprinkle your code with generous amounts of Debug.Log() and use your imagination to figure out whats going on
You can double click on what’s logged, in the console, and your code editor will be opened up with the line that contains the relevant Debug.Log/print selected.
I tried to work the assignment of existing material to a game object in hierarchy.
Material ballmat = new Material("ballmat"); //line 13
GameObject ball = GameObject.Find("SphereBall");
ball.renderer.material = ballmat;
It shows error:
ShaderLab error: Syntax error in : ballmat at line 1
UnityEngine.Material:.ctor(String) (at /Users/build/builds/unity-iphone-1.6/iphone-1.6/Runtime/Export/Generated/Graphics.cs:1167)
AssignShader:Start() (at Assets/Scripts/AssignShader.cs:13)
[/Users/build/builds/unity-iphone-1.6/iphone-1.6/Projects/…/External/shaderlab/Library/ShaderLabErrors.cpp line 33]
Then how should I imagine the bug? I cannot see the variable’s value.
ballmat is a material created in Asset and it has shader.
I saw http://answers.unity3d.com/questions/5965/how-can-i-assign-materials-using-c-code but did not work
try
Material mymateriel=new Material(Shader.Find(“ballmat”));
Dear Sir
ballmat is a Material, it has shader itself.
ballmat is not a shader.
Your script has error:
NullReferenceException
UnityEngine.Material…ctor (UnityEngine.Shader shader) (at /Users/build/builds/unity-iphone-1.6/iphone-1.6/Runtime/Export/Generated/Graphics.cs:1168)
InstantiateBoids.Start () (at Assets/Scripts/InstantiateBoids.cs:7)
you can not use materials by name.
you have to find their objects to assign them as material etc.
As such what you want to do is either link them to the script through a public variable or you must put it into the Resources folder you would create so you can get it with Resources.Load
A sidenote on the original title: Even if unity had breakpoints and debugging, in this case it wouldn’t change anything. Breakpoints are used at runtime but you have a syntax error which means that your code would not even compile, not considering running at all
Dear Sir
I tried your suggestion and here is the code:
using UnityEngine;
using System.Collections;
public class InstantiateBoids : MonoBehaviour {
public Material ballmaterial;
void Start () {
//ballmaterial = new Material(Resources.Load("ballmat")); //does not work
ballmaterial = Instantiate(Resources.Load("ballmat")) as Material;
Transform ball = transform.Find("BallsBoid/Ball");
ball.renderer.material = ballmaterial;
}
}
You can see that the ballmat material only be clone to the first BallBoid/Ball object.
Other BallBoid/Ball objects have not been cloned with ballmat material.
Could you give me some hints how can I do it?
In previous thread http://forum.unity3d.com/viewtopic.php?p=317559#317559
Did I lose the connection and how should I fix it?
I think you got quite a bit mixed up here.
for example materials are never clones of game objects / transforms
As for your code: Assigning the material to one instance of the ball will not affect any of the others.
You could naturally load it for every ball above way but do not do that, that will disable batching!
What you want to do is to create a ball prefab full setup and then drag that into the level / instantiate that one, then all are on the same material.
alternatively, make some other code assign the material and then to all existing balls in one go
I created a BallsBoid prefab under Prefabs folder.
I assigned the following script to BallsBoid prefab.
using UnityEngine;
using System.Collections;
public class InstantiateBoids : MonoBehaviour {
public Material ballmaterial;
void Start () {
ballmaterial = Instantiate(Resources.Load("ballmat")) as Material;
Transform ball = transform.Find("BallsBoid/Ball");
ball.renderer.material = ballmaterial; //line 9
}
}
I drag it to hierarchy under BoidsGroup.
All BallsBoid under BoidsGroup have that script.
When I run it, it throws error:
NullReferenceException
InstantiateBoids.Start () (at Assets/Scripts/InstantiateBoids.cs:9)
I think the code transform.Find(“BallsBoid/Ball”); is wrong.
How can I access it properly? Should I put it in Resources folder like ballmat?
If so, how’s the code to be?
I re-think your suggestion these 2 days:
What you want to do is to create a ball prefab full setup and then drag that into the level / instantiate that one, then all are on the same material.
Since I need to use code to assign the material to a ball prefab in runtime, the ball prefab cannot be full setup.
alternatively, make some other code assign the material and then to all existing balls in one go
This solution is feasible. How do you iterate all ball game objects in hierarchy and assign the material to their renderer.material?
The easiest way to get all the ball objects is to give them all the same tag and use GameObject.FindGameObjectsWithTag to retrieve them:-
var balls: GameObject[] = GameObject.FindGameObjectsWithTag("Ball");
Dear Sir
I tried
public Material ballmaterial;
void Start () {
ballmaterial = Instantiate(Resources.Load("ballmat")) as Material;
GameObject[] balls = GameObject.FindGameObjectsWithTag("Ball");
//Transform ball = transform.Find("BallsBoid/Ball");
balls.renderer.material = ballmaterial;
}
Errors:
UnityEngine.GameObject[ ]' does not contain a definition for
renderer’
UnityException: Tag: BallsBoid/Ball is not defined!
Dear Sir
After selecting a tag “Respawn” and your code works now.
Thank you so much.
By the way, when I add a new tag, it opens a tag manager. There are builitin layer and user layer. Also, I did not see the tag name Respawn.
So, what is the proper way to add the tag name ‘ball’?
There is a Tags foldout at the top of the list above all the layer names. The last tag in the list should always be empty. Start typing there to create a new tag name, and a new empty tag will be created too.